问题
Hello and thanks for help I have asked here how to Polpulate listview from firebase and thankfully Krishna Kishan solved my problem but now what I like to do is when I click on listview items, that item will be removed, that is ok I done this part but my problem is I want to delete or remove associated data with that particular item in the list view from firebase data here is my code
listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapt.notifyDataSetChanged();
myRef.getRoot().child("message").removeValue();
}
});
But this line of code remove everything from firebase, but I want only remove the associated data with listview item.
myRef.getRoot().child("message").removeValue();
I illustrate more. Let ssay on each click I add a value to a firebase let say 1, then I add 2 then 3, so my listview items will be 1,2 and 3 now when I click on 2 on listview I want to delete 2 from listview and from firebase as well and leave 1 and 2 , but I do not want to call it by name because if I have large listview let say 20 items I want a user click on any items then remove them from list and firebase so next time when they relaunch the app that list view will not show because it has removed from firebase
Here's my firebase database structure:
回答1:
You have to modify some code that is outside the snippet you posted.
I guess your items
are read from the database. When you read your "message" node you have to save keys as well as values.
You can achieve this in a number of ways. e.g.
final ArrayList<String> keyList = new ArrayList<>();
final ArrayList<String> items = new ArrayList<>();
myRef.getRoot().child("messages")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messages : dataSnapshot.getChildren()) {
keyList.add(messages.getKey());
items.add(messages.getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
/*handle errors*/
}
});
Now you have both keys and values. Modify your OnClickListener this way:
listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapt.notifyDataSetChanged();
//new code below
myRef.getRoot().child("message").child(keyList.get(position)).removeValue();
keyList.remove(position);
}
});
Obviously listi.setOnItemClickListener( ... )
should be called when both keyList
and items
are properly filled. Please notice that ValueEventListener
is asynchronous.
回答2:
I assume you are using push() to add those messages to the database. Since push() generates a time based id, the order of the elements in the list is chronological, meaning what was added later comes after what was added earlier. So, when you are push()-ing, get the key and maintain a list of the ids. Then, in your onClick, use the position parameter to get the id for the clicked item and delete it in the database. Here's an example:
List<String> ids; //update this list with ids as you push
..
//then in your onItemClick
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
rootReference.child("messages").child(ids.get(position)).removeValue();
//update any offline lists
}
Please note that this assumes you're also displaying the messages in your ListView in the same order as in your database.
来源:https://stackoverflow.com/questions/44700447/when-click-on-listview-delete-it-and-delete-associated-data-with-it-in-firebas