Pass Firebaserecycleradapter values

Asked

Viewed 160 times

1

How do I pass an object by clicking on a Recycler view item while using a FirebaseRecyclerAdapter?

The way I’m doing it is working, I create a list, and in the method populateViewHolder i add the object I want to pass to a list, so in the onclick method I pass the object according to the position

noteAdapter = new FirebaseRecyclerAdapter<Note, NoteHolder>(Note.class, R.layout.note_item, NoteHolder.class, dbRef) {

            @Override
            protected void populateViewHolder(NoteHolder viewHolder, Note model, final int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setContent(model.getContent());
                Log.d(TAG, "!!!!!!!!!!!!!");
                model.setId(getRef(position).getKey());
                noteList.add(model);

                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent it = new Intent(v.getContext(), NoteDetailActivity.class);
                        it.putExtra(MainFragment.NOTE_PARCE, noteList.get(position));
                        startActivity(it);
                    }
                });
            }
        };

But there’s a problem... populateViewHolder is called whenever the user uses the scroll of RecyclerView, ie, is added in the list the same values whenever the scroll is used!

What is the best way to pass the object to the other Activity?

Or how not to add the same objects whenever the user uses the scroll?

UPDATE:

I did it inside the populateViewHolder:

if(noteList.size()<this.getItemCount()) {
     noteList.add(model);
}

It solves the problem of adding repeated objects, but there is one though, whenever the Adapter is updated, it will add more objects... Firebase stores the data in cache, when online it sends it to base in firebase, if the data is cached and I remove all in the firebase console, the List is not updated, continues with the data that was cached, even if I add new ones. How to clear List when data changes? I tried at the onDataChanged(), but it is called whenever a value is added to the Adapter, that is, it will always clear the entire list, even if it is just an added value and not all removed (as I did in the example)!

@Override
protected void onDataChanged() {
  super.onDataChanged();
  noteList.clear();
}

1 answer

2


Problem solved, I did so:

@Override
protected void onDataChanged() {
super.onDataChanged();
  if(getItemCount()==0) {
      noteList.clear();
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.