简体   繁体   中英

Retrive user uid details in onBindViewHolder RecyclerView Firebase Adapter | FirebaseUI | Android ,Java

here i face a problem with 'holder' as i fast scroll,holder position gets updated (because of recycling behaviour) and after a while onSuccess gets called(as it takes some time for fetching data) until then the holder position already changed and holder.mUserFullName.setText(user.getName()) applied to wrong holder position. This only happens when i fast scroll, if i disable recycling then it solved the problem but i want recycling feature.

@Override
        protected void onBindViewHolder(@NonNull PostVH holder, int position, @NonNull Post post) {

            MyFirestoreDbRefs.getAllUsersCollection().document(post.getByUid())
                    .get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    User user=documentSnapshot.toObject(User.class);

                    /*here i face a problem with 'holder' as i fast scroll,holder position gets updated
                    * (because of recycling behaviour) and after a while onSuccess gets called(as it takes some time for fetching data) 
                    * until then the holder position already changed and holder.mUserFullName.setText(user.getName()) applied to wrong 
                    * holder position */

                    holder.mUserFullName.setText(user.getName());
                }
            });

        }

Post Collection Structure in DB Users Collection Structure in DB

The below solution, might work it worked for some cases:

Call this on your adapter :

adapter.setHasStableIds(true);

In your adapter override 2 methods, under onBindViewHolder() :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM