简体   繁体   中英

How to delete an item from ListView and also delete it on Firebase?

My problem is that if I try to delete an item on my ListView , all the data will be deleted to on both listview and my firebase realtime database. All I want is, if I long click an item, only that item will be removed, not all of them on the ListView . Thank you. Any help is appreciated

This is what I declare:

 MatkulAdapter matkulAdapter = null;
 Matkul matkul;
 DatabaseReference root = FirebaseDatabase.getInstance().getReference();
 DatabaseReference dbReference = root.child("MataKuliah");

This is the code to delete an item:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    Matkul item = matkulAdapter.getItem(position);
                    matkulAdapter.remove(item);
                    dbReference.removeValue();
                    matkulAdapter.notifyDataSetChanged();
     return true;
                }
            });

This is my database:

This is my database

EDIT QUESTION

and also i tried this code with.child but the data only deleted on ListView. So the data comes back when i re-open the ListView.

   listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Matkul item = matkulAdapter.getItem(position);
                matkulAdapter.remove(item);
                dbReference.child(item.getNamaMatkul()).removeValue();
                matkulAdapter.notifyDataSetChanged();

When you are using the following line of code:

dbReference.removeValue();

Everything beneath MataKuliah will be deleted. If only need to remove the 1595588052231 child, then simply use the following line of code:

dbReference.child("1595588052231").removeValue();

A better approach might be to store the 1595588052231 as a value of a uid property in your database and then use:

dbReference.child(item.getUid()).removeValue();

you need pass your firebase item id (15955880552231) in second child, like this code:

Query query = rdbReference.orderByChild("namaMatkul").equalTo(item.namaMatkul);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
        appleSnapshot.getRef().removeValue();
        matkulAdapter.remove(item);
        matkulAdapter.notifyDataSetChanged();
    }
}

@Override
public void onCancelled(DatabaseError databaseError) {
    Log.e(TAG, "onCancelled", databaseError.toException());
}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM