When click on listview , delete it and delete associated data with it in firebase

前端 未结 2 857
忘了有多久
忘了有多久 2021-01-23 16:26

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

2条回答
  •  深忆病人
    2021-01-23 17:15

    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 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.

提交回复
热议问题