2

enter image description here

As you can see from the picture I have a child FreezerItems, and under that child I have two other childs which are randomKeys that Firebase has created by using push(). My question is, how do I specifically get the key L8i2M4wNUF5wOojaFE. Also how do I get that key into my RecyclerViewAdapter. I have tried this:

holder.mDeleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabase = FirebaseDatabase.getInstance().getReference().child("FreezerItems");
            String key = mDatabase.push().getKey();
            Toast.makeText(view.getContext(), key, Toast.LENGTH_SHORT).show();
        }
    });

But all that ever did was show a Toast with a random key, that changes each time I click on the button

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Kristofer
  • 809
  • 9
  • 24

3 Answers3

1

What you are doing in your code, is that you are generating each time you click on the item a new id. You are not using the one that is stored in your database. In order to use the id that was already generated once, you need to store it first. So to solve this problem, I recommend you store that random generated id provided by the push() method as a property of your model class. You database structure should look like this:

Firebase-root
    |
    --- FreezerItems
            |
            --- L8i2M4wNUF5wOojaFE
                      |
                      --- date: "3/31/2018"
                      |
                      --- name: "Ice"
                      |
                      --- freezerItemId: "L8i2M4wNUF5wOojaFE"

Then to remove that particular item, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference freezerItemsRef = rootRef.child("FreezerItems");
holder.mDeleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            freezerItemsRef.child(freezerItems.getId()).removeValue();
        }
    });

In which freezerItems is the object of your model class and getId() is a public getter that returns that particular id.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you very much – Kristofer Mar 29 '18 at 15:49
  • I have one last question though, when I delete the view it deletes in Firebase but I have to reload it again to see the change. How do I fix this? I tried putting notifyDataSetChanged() but nothing happened. – Kristofer Mar 29 '18 at 16:03
  • 1
    Yes, you need to notify the adapter for that particular change. `notifyDataSetChanged() ` should solve your problem. If you want another way to display data, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. It will handle all operation for you automatically. So the adapter will refreshed automatically when you add/delete/update an item. – Alex Mamo Mar 29 '18 at 16:07
0

Event objects should contain id, to populate id of the item when you read data populate, do like this..

   for (DataSnapshot adSnapshot: dataSnapshot.getChildren()) {
        Event eobj = adSnapshot.getValue(Event.class);

                String key = dataSnapshot.getKey();
        eobj.setItemKey(key);
        mDataSet.add(eobj);
   }

using the id of item you can delete it when button is clicked. Do something like this...

FirebaseDatabase.getInstance().getReference()
                    .child("FreezerItems").child(product.getId).removeValue()
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                //remove item from list and refresh recyclerview
                                mDataSet.remove(position);
                                notifyItemRemoved(position);
                                notifyItemRangeChanged(position, mDataSet.size());
                            } else {
                                Log.d("Delete Item", "couldn't be deleted");
                            }
                        }
                    });

For complete example, you can see http://www.zoftino.com/firebase-realtime-database-android-example

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31
  • I have another child under Freezer Items which is the random key from using push() how do I get that? – Kristofer Mar 29 '18 at 04:07
  • It should be part of your item data, when you fetch need to populate it. – Arnav Rao Mar 29 '18 at 04:21
  • for (DataSnapshot adSnapshot: dataSnapshot.getChildren()) { Event eobj = adSnapshot.getValue(Event.class); String key = dataSnapshot.getKey(); eobj.setItemKey(key); mDataSet.add(eobj); } – Arnav Rao Mar 29 '18 at 04:28
  • by setItemKey do you mean set_id? also if it is set_id it shows a NullPointerException warning – Kristofer Mar 29 '18 at 04:36
0

I think it still didn't explain how do we get that id that is generated randomly. I have many more elements when I say datasnapshot.getKey() it will return the top node but I want access nodes below this top node those are random keys. And each random key have some other values. I belive we have to do something before .push() to get that key. Later to save in model

user---
     -sSHSsdlkjjfh..
     -sSHSLSHBSB...
     -sSHSLKJSSKJ...
baltekg
  • 985
  • 9
  • 31
devos
  • 1