Kristofer
Kristofer

Reputation: 817

FirebaseRecyclerView updating wrong items

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.

Upvotes: 0

Views: 238

Answers (1)

SUPERCILEX
SUPERCILEX

Reputation: 4007

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()

Upvotes: 2

Related Questions