0

I have a RecyclerView and you can add/delete items there. My problem is that the adapter shows the wrong item (does not update the item) that I selected to delete. I mean, in my database it deletes the right item, but on the application it's wrong unless I restart the activity then it'll update and show the correct items. I think this might actually be a glitch with the FirebaseAdapter. I'm not so sure but anyway this is my code.

FirebaseRecyclerOptions<Event> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Event>()
            .setQuery(query, Event.class).build();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, ProductHolder>(firebaseRecyclerOptions) {
        @Override
        protected void onBindViewHolder(final ProductHolder holder, final int position, Event model) {
            model = mDataSet.get(position);
            holder.mItemName.setText(model.getName());
            holder.mItemDate.setText(model.getDate());
            final Event finalModel = model;
            holder.relativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mDatabaseFreezer.child(finalModel.get_id()).removeValue(); //this is how I remove the item
                    return true;
                }
            });

}

        @Override
        public ProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recycler_view_row_snippet,parent,false);

            return new ProductHolder(view);
        }
    };

I also tried to use notifyDataSetChange but I don't think the FirebaseAdapter actually needs it.

Update

This is how I get the data from my database

 mDatabaseFreezer.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            HashMap<String, String> value = (HashMap<String,String>) dataSnapshot.getValue();
            if (value != null) {
                String name = value.get("Name");
                String date = value.get("Date");
                String key = value.get("Key");
                String productAmount = value.get("Amount");

                Event event = new Event();
                event.setId(key);

                mDataSet.add(new Event(name,date, key, productAmount));

            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

2nd Update

So my code was right all along I just didn't include the code where I sort the ArrayList based on dates which messed up the order of the items therefore deleting the wrong items.

Kristofer
  • 809
  • 9
  • 24
  • 1
    Not sure about the answer but I'd move setting any onClickListeners into the onCreateViewHolder method instead of making everything final. Better for performance. You can get the clicked position with getAdapterPosition(). – user1504495 May 26 '18 at 22:09
  • 1
    I haven't used firebase sdk recently and things may have changed but normally you delete data from the adapter. Something similar to this: firebaseRecyclerAdapter.getRef(position).removeValue(); and maybe you have to registerAdapterDataObserver on the adapter (firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { ...}); – IldiX May 26 '18 at 23:41
  • Have you solved the problem? – Alex Mamo May 27 '18 at 07:18
  • @AlexMamo not yet – Kristofer May 27 '18 at 16:59

1 Answers1

2

You can't use any data from the onBindViewHolder method in a click listener because it's only valid during that first tick. Get your model like this instead:

getItem(holder.getAdapterPosition()).get_id()
SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61
  • I can't use get_id() using that method because as you can see my my Model is also a class. Do you know how I can get through this? – Kristofer May 27 '18 at 03:08
  • 1
    `getItem(holder.getAdapterPosition())` will return an `Event` so `get_id()` _will_ be available. Unless there's a compiler error? – SUPERCILEX May 27 '18 at 05:32
  • your right, so I did `String t = getItem(holder.getAdapterPosition()).get_id();` and the String returns null for some reason – Kristofer May 27 '18 at 16:59
  • That's probably because your db isn't mapping correctly to your model. Use `setId(String)` and `getId()` instead of the underscore stuff. Oh, and make sure your fields are private. – SUPERCILEX May 27 '18 at 17:46
  • I've checked everything so that I have setId in my model and changed set_id and get_id into setId and getId also made sure the fields are private but it still returns null. I've updated my question so that you can see how I set my Id – Kristofer May 27 '18 at 18:28
  • Ohhhhhhhhh, yeah, no, you definitely shouldn't be using the child even listener thing since FirebaseUI does that for you. Rename the methods to `getKey` and `setKey` and remove the data set stuff. You might also need to change your DB to have lowercase properties. – SUPERCILEX May 27 '18 at 19:18
  • Damn I'm so sorry I wasted so much of your time, I found out the answer though. I didn't include the code where I sort the arrayList based on the dates, after removing that it wokrs fine – Kristofer May 27 '18 at 20:29