-1

I'm trying to load data from firebase to recyclerView using the code below but, I'm Having a problem when scrolling RecyclerView after scrolling down a lot of time successively, and that happened only in the first time when data load, i really don't know what's going wrong in my code please help!

Here it is a summarize of my onBindViewHolder :

 holder.tvPhoneNumber.setText("");
 holder.ivContactImage.setImageResource(R.color.avatar_color);

 name = FirebaseDatabase.getInstance().getReference().child("Users").child(phoneNumberId);
 name.keepSynced(true);

 followListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.child("status").exists()) {
                            nameData = dataSnapshot.child("status").getValue().toString();
                            holder.tvPhoneNumber.setText(nameData);
                        } else {
                            holder.tvPhoneNumber.setText("");
                        }
                        if (dataSnapshot.child("thumb_image").exists()) {
                            imageData = dataSnapshot.child("thumb_image").getValue().toString();
                            Picasso.with(mContext).load(imageData).resize(100, 100).placeholder(R.color.avatar_color)
                                    .error(R.color.avatar_color).centerCrop().into(holder.ivContactImage);
                        } else {
                            holder.ivContactImage.setImageResource(R.color.avatar_color);

                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                };
                name.addListenerForSingleValueEvent(followListener);

The problem is that i have this methode inside of my adapter class

@Override
    public int getItemViewType(int position) {

        if (position == contactList.size()){
            return VIEW_TYPE2;
        }else {
            return VIEW_TYPE1;
        }
    }
naima
  • 129
  • 1
  • 12
  • 1
    RecyclerView recycles rows which is why a listener attached this way will give unexpected results. Load the data first then hand it off to the recyclerview adapter – kandroidj Mar 22 '18 at 22:43

1 Answers1

0

I believe that inner_class7 is right, you are loading the data and binding it at the same time. Load it first and then bind. But maybe this may help in solving your problem, override these methods in your adapter:

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

But absolutely do follow inner_class7 suggestion first.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • The problem is, I already had getItemViewType methode, I updated the question. what can i do in this case? – naima Mar 24 '18 at 18:07
  • Did you try to load the data outside of onBindViewHolder first? – Suleyman Mar 24 '18 at 18:51
  • Fist of all, Thank you for the answer, I can't load data outside of onBindViewHolder because the firebase reference, I mean the child(phoneNumberId) change on every position. because what i'm trying do is, to take the number of the contact inside my listContact and then fetch the data of this contact inside of firebase. – naima Mar 24 '18 at 19:00
  • @naima you are welcome. I am sorry if I'm not being helpful. I found this [link](https://stackoverflow.com/questions/34962254/how-to-retrieve-data-to-a-recycler-view-from-firebase), it may help. Further than that I do not know, sorry :) – Suleyman Mar 24 '18 at 22:44