Pass values from FirebaseRecyclerAdapter

2

How do I pass an object when I click on a recycler view item and am using a FirebaseRecyclerAdapter ?

The way I'm doing is working, I create a list, and in the populateViewHolder method I add the object I want to pass in a list, then in the onclick method I pass the object according to position p>

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 this has a problem ... populateViewHolder is called whenever the user uses the RecyclerView scroll, ie the same values are added to the list whenever the scroll is used!

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

Or how do you not to add the same objects every time the user scrolls?

UPDATE:

I did this within populateViewHolder :

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

It solves the problem of adding repeating objects, but it does have one however, whenever the adapter is updated, it will add more objects ... Firebase caches data, when it is online it sends to 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 though I add new ones. How to clear the List when the data changes? I tried in onDataChanged() , however it is called whenever a value is added in the adapter, ie, it will always clean the entire list, even if it is just a value added and not all removed (as I did in the example)!

@Override
protected void onDataChanged() {
  super.onDataChanged();
  noteList.clear();
}
    
asked by anonymous 06.06.2017 / 05:29

1 answer

3

Problem solved, I did so:

@Override
protected void onDataChanged() {
super.onDataChanged();
  if(getItemCount()==0) {
      noteList.clear();
  }
}
    
09.06.2017 / 04:25