Skip to content Skip to sidebar Skip to footer

Item Gets Duplicated In Adapter, When We Delete Or Update Item From Firebase Realtime Database

Items in adapter gets duplicated 2 or 3 times when we try to delete item from list in firebase realtime database. Last item in adapter didnt gets removed after we perform deletion

Solution 1:

1) You can try swap i.e

publicclassTestApdaterextendsRecyclerView.Adapter<....> {    
...
privatestatic List<Mobel> mData;
...    
publicvoidswap(List list){
        if (mData != null) {
            mData.clear();
            mData.addAll(list);
        }
        else {
            mData = list;
        }
        notifyDataSetChanged();
}

2) Or if this also doesn't work you can simple add this on your on click for your delete button , please note its not the best solution but works .

    Intent i=newIntent(this, SameActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_NEW_TASK|
FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

Solution 2:

This Solution work for me :

This code will remove last item in adapter after performing deletion operation in firebase realtime database.

mAcceptedList.removeAt(adapterPosition); mShortlistProfileAdapter.notifyDataSetChanged()

overridefunonItemClicked(
    adapterPosition: Int,
    mUserListProfile: ArrayList<UserProfile>
) {
           mShortlistedProfileVM?.deleteUserProfile(mUserListProfile.get(adapterPosition))
            mAcceptedList.removeAt(adapterPosition);
            mShortlistProfileAdapter.notifyDataSetChanged()
        }
}
  1. To Prevent item gets duplicated.

Clear your list before adding data into list.

mAcceptedList.clear()    <---addthis line of code. 

val listSize = etrieveDataResponse.userViewedProfile!!.mViewedProfileList.size
 val mListUserProfile = retrieveDataResponse.mViewedProfileList
 for (i in0..listSize - 1) {
  if (mListUserProfile.get(i).mProfileStatus == true) {
  mAcceptedList.add(mListUserProfile.get(i))
 }

} showDataOnAdapter(mAcceptedList)

Post a Comment for "Item Gets Duplicated In Adapter, When We Delete Or Update Item From Firebase Realtime Database"