我有一个RecyclerView
我更新了适配器中的数据,我的问题是更改后我无法刷新RecyclerView
数据。
我使用notifyDataSetChanged();
但是不起作用
UpdateStudent编辑学生姓名,问题是在更改后,RecyclerView
需要刷新列表
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);
}
}
@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
}
}
}
答案 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);
}
}