【问题标题】:Item gets duplicated in adapter, when we delete or update item from Firebase Realtime Database当我们从 Firebase 实时数据库中删除或更新项目时,项目在适配器中重复
【发布时间】:2019-12-18 05:45:07
【问题描述】:

当我们尝试从 firebase 实时数据库的列表中删除项目时,适配器中的项目会重复 2 或 3 次。

我们在 firebase 数据库中实时删除后,适配器中的最后一项没有被删除。

试过 notifyDataSetChanged()。

override fun onItemClicked(
    adapterPosition: Int,
    mUserListProfile: ArrayList<UserProfile>
) {
     mShortlistedProfileVM?.deleteUserProfile(mUserListProfile.get(adapterPosition))
            mShortlistProfileAdapter.notifyDataSetChanged()
        }

}

适配器中的最后一项应该被删除,在我们执行删除 [firebase 实时数据库中的操作后。并且应该删除重复的项目。

【问题讨论】:

  • 分享你的听众的代码

标签: android firebase firebase-realtime-database


【解决方案1】:

1)您可以尝试交换即

public class TestApdater extends RecyclerView.Adapter<....> {    
...
private static List<Mobel> mData;
...    
public void swap(List list){
        if (mData != null) {
            mData.clear();
            mData.addAll(list);
        }
        else {
            mData = list;
        }
        notifyDataSetChanged();
}

2) 或者如果这也不起作用,您可以在单击删除按钮时简单地添加它,请注意它不是最好的解决方案,但有效。

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

【讨论】:

    【解决方案2】:

    这个解决方案对我有用:

    此代码将在 Firebase 实时数据库中执行删除操作后删除适配器中的最后一项。

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

    override fun onItemClicked(
        adapterPosition: Int,
        mUserListProfile: ArrayList<UserProfile>
    ) {
               mShortlistedProfileVM?.deleteUserProfile(mUserListProfile.get(adapterPosition))
                mAcceptedList.removeAt(adapterPosition);
                mShortlistProfileAdapter.notifyDataSetChanged()
            }
    }
    
    1. 防止项目重复。

    在将数据添加到列表之前清除您的列表。

    mAcceptedList.clear()    <---add this line of code. 
    
    val listSize = etrieveDataResponse.userViewedProfile!!.mViewedProfileList.size
     val mListUserProfile = retrieveDataResponse.mViewedProfileList
     for (i in 0..listSize - 1) {
      if (mListUserProfile.get(i).mProfileStatus == true) {
      mAcceptedList.add(mListUserProfile.get(i))
     }
    

    } showDataOnAdapter(mAcceptedList)

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多