簡體   English   中英

如何使用itemHolder更新RecyclerView

[英]How to Update RecyclerView With itemHolder

我想在單擊按鈕后使用循環更新所有RecyclerView項。 但是所有項目都設置有Last循環值。

RecyclerView適配器:

delete_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    if(dateSelected != null)
     {
         database.deleteObject(checkOut, classId, studentId, dateSelected);

        for (int i = 0 ; i < tableData.size() ; i++)
        {    
            int classId = tableData.get(i).getClassId();
            String studentId = tableData.get(i).getStudentId();
            int pCount = database.PositiveCounter(classId , studentId);

            itemHolder.studentPositive.setText(String.valueOf(pCount));
       }

  }

要更新RecyclerView,您需要先更新包含數據的列表,然后再調用notifydatasetchanged()。

例如

public void changeDataSet(List<Student> student) {
adapter.setList(student);
adapter.notifyDatasetChanged();
}

setList將成為適配器的一部分,如下所示:

public void setList(List<Student> student) {
this.student = student;
}

如果刪除按鈕是持有人內部itemView的一部分,則需要從適配器向活動添加回調。

在您的活動中,在onClick on按鈕中使用以下代碼

mAdapter.removeItem(position);

在Recycler View適配器類中,使用添加removeItem方法,如下所示

public void removeItem(int position) {
    mList.remove(position);
    notifyItemRemoved(position);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM