如何在更新后刷新RecyclerView项目

时间:2018-04-01 12:02:17

标签: android android-recyclerview recycler-adapter notifydatasetchanged

我有一个RecyclerView我更新了适配器中的数据,我的问题是更改后我无法刷新RecyclerView数据。

我使用notifyDataSetChanged();但是不起作用

UpdateStudent编辑学生姓名,问题是在更改后,RecyclerView需要刷新列表

ReyclerView适配器:

public class RecyclerViewAdapter_StudentList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<StudentTable> tableData = new ArrayList<>();

    Context context;

    private LayoutInflater mInflater;

    public RecyclerViewAdapter_StudentList(Context context, List<StudentTable> tableData) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        this.tableData = tableData;
    }

    public class itemHolder extends RecyclerView.ViewHolder {

        TextView studentName;

        return new itemHolder(view);
    }
}

onBindViewHolder

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    itemHolder itemHolder = (itemHolder) holder;

    String studentName = tableData.get(position).getStudentName();

    itemHolder.studentName.setText(studentName);

    itemHolder.editStudent_Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {           
            String studentName_new = studentName_EditText.getText().toString();

            database.UpdateStudent(classId , studentName_new);

            notifyDataSetChanged();// does not work           
        }      
    }
}

3 个答案:

答案 0 :(得分:0)

此处您仅通知适配器而不更改数据列表,因此您需要这样做 更新表格中的检索数据并将其存储在当前列表tableData中,然后您必须致电notifyDataSetChanged();

答案 1 :(得分:0)

试试这个:

itemHolder.editStudent_Button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {           
        String studentName_new = studentName_EditText.getText().toString();            
        database.UpdateStudent(classId , studentName_new);

        StudentTable student = tableData.get(position);
        student.setName(studentName_new);
        tableData.set(position, student);
        notifyItemChanged(position);          
    }      
}

答案 2 :(得分:0)

itemHolder.editStudent_Button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {           
        String studentName_new = studentName_EditText.getText().toString();            
        database.UpdateStudent(classId , studentName_new);

        StudentTable student = tableData.get(position);
        student.setName(studentName_new);
         notifyItemChanged(position);

    }      
}