Remove ListView items in android from firebase realtime database

Viewed 98

May you help us with our school project it's due Thursday. we have a problem when we use push() to insert data to the Firebase Realtime Database we trying to remove values from randomly generated key and it works but only removed from the app but NOT in Realtime Database we can't figure out what is the problem, please we need your help

push() --> in Additem.class

private void StoreProductInformation() {

    Description = InputProductDescription.getText().toString();
    Pname = InputProductName.getText().toString();


    citem obj = new citem(Pname,Description );
    ProductsRef.push().setValue(obj);

    Toast.makeText(Additem.this, "Added To cart", Toast.LENGTH_SHORT).show();
    Intent i =new Intent(this,shoppingcart.class);
    startActivity(i);

}

removeValue() --> in shoppingcart.class

list.setAdapter(myadapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            AlertDialog.Builder adb=new AlertDialog.Builder(shoppingcart.this);
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete " + position);
            final int positionToRemove = position;
            adb.setNegativeButton("Cancel", null);
            adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    mylist.remove(positionToRemove);
                    myadapter.notifyDataSetChanged();
                    //myRef.child("key").removeValue();

                }});
            adb.show();
        }
    });

realtime database

1 Answers

The simplest solution in which you can solve the problem would be to store that pushed ID as property under each object:

Firebase-root
  |
  --- shoppingcart
        |
        --- -N1kC...ZKwO 
        |     |
        |     --- name: "R"
        |     |
        |     --- qty: 3
        |     |
        |     --- key: "-N1kC...ZKwO" 
        |
        --- -N1kC...LO3- 
              |
              --- name: "Z"
              |
              --- qty: 10
              |
              --- key: "-N1kC...LO3-" 

This also means that the class that holds those two fields (name and qty) should now also contain another field called key. So assuming that this class is called Item, to remove the clicked item from the adapter, as well from the database, please use the following lines of code:

public void onClick(DialogInterface dialog, int which) {
    //Remove the item from the Realtime Database
    Item clickedItem = mylist.get(positionToRemove); 
    myRef.child(clickedItem.getKey()).removeValue(); 

    //Remove the item from the adapter.
    mylist.remove(positionToRemove);
    myadapter.notifyDataSetChanged();
}});

This will work if you have added public getters inside your class, otherwise, if you have access to the public field, then please use:

myRef.child(clickedItem.key).removeValue();
                        

If you, however, consider at some point in time try using Cloud Firestore and a RecyclerView instead of a ListView, then please check one of the following articles which you consider to be more appropriate for your use-case:

Or:

Related