1

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();
divaniaf
  • 13
  • 4

2 Answers2

0

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();
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thankyou sir, but what if i want to delete random item on the MataKuliah? i try this code `dbReference.child(item.getUid()).removeValue();` but having error on the getUid() – divaniaf Jul 24 '20 at 12:58
  • You have an error because you should add in your `Matkul` class a new property named `uid` and the corresponding `getUid()` getter. In your database under your `1595588052231` node, you should also have the `uid` property that should hold the same `1595588052231` value. Then everything will work fine. If you need to get a random value, please check **[this](https://stackoverflow.com/questions/50413117/how-to-get-unique-random-product-in-node-firebase/50413208)** out. – Alex Mamo Jul 24 '20 at 13:03
  • i already add the `private String uid` and getter the `getUid()` on `Matkul` sir, but get this error `Can't pass null for argument 'pathString' in child()` – divaniaf Jul 24 '20 at 13:17
  • This is happening because the value that you are trying to get from the database does **not** exist. As I said earlier, you should also add it to the database. So you have `namaDosen` and `namaMaktul`, add and the `uid` with the value of `1595588052231`. – Alex Mamo Jul 24 '20 at 13:19
0

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());
}});
Felipe Palma
  • 378
  • 1
  • 9
  • thankyou sir, but sorry, how to get the firebaseItemID? How should i declare it? – divaniaf Jul 24 '20 at 12:59
  • Use the code in your click listener to find specific item – Felipe Palma Jul 24 '20 at 13:42
  • thankyou so much sirr! the code is working but having a little bug that if i delete an item, it will be change to other data or the data will be double, but when i go back first then open it again, it would gone from the listview. Many thanks sirr – divaniaf Jul 24 '20 at 14:03
  • This is only snippet, you need to develop the logic ;) – Felipe Palma Jul 24 '20 at 14:05