问题
I have a RecyclerView
in AdapterActivity
. After clicking on any of its items, I update that item using my AlertDialogShow#UpdateStudent()
method. My problem is that I can not refresh the Adapter
inside the UpdateStudent()
method using notifyItemChanged()
.
How can I refresh the Adapter
from another class that does not have direct access to the Adapter
?
In the AdapterActivity
with the RecyclerView
:
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialogShow show =
new AlertDialogShow(context, database, studentData, performanceData);
show.UpdateStudent(studentName, studentId, classId, position);
}
}
}
The AlertDialogShow
class:
public class AlertDialogShow {
Context context;
DatabaseHandler database;
List<StudentTable> studentData;
public AlertDialogShow(Context context, DatabaseHandler database,
List<StudentTable> studentData , List<PerformanceTable> performanceData) {
this.context = context;
this.database = database;
this.studentData = studentData;
this.performanceData = performanceData;
}
public AlertDialog UpdateStudent(final String studentName, final String studentId,
final int classId , final int position) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
View viewLayout = LayoutInflater.from(context)
.inflate(R.layout.alertdialog_edit_class_or_student , null);
dialog.setView(viewLayout);
final AlertDialog alertDialog = dialog.create();
Button editStudent_btn = (Button)viewLayout.findViewById(R.id.item_btn_EditClassStudent_Edit);
editStudent_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
database.UpdateStudentDatabase(classId , studentId_new , studentId, studentName_new);
StudentTable studentTable = studentData.get(position);
studentTable.setStudentName(studentName_new);
studentTable.setStudentId(studentId_new);
studentData.set(position , studentTable);
//notifyItemChanged(position); <-- cannot define this line
}
}
);
return alertDialog;
}
}
回答1:
Updating RecyclerView
adapter can only be done in adapter itself or through adapters instance in your Activity
. To reach those methods you need to use interfaces like so:
public class AlertDialogShow {
public interface OnItemChange {
void notifyAdapter(int position);
}
private OnItemChange listener;
public AlertDialogShow(...) {
this.listener = (MyActivity)context;
}
}
Then write an implementation for OnItemChange interface in your Activity
like so:
public class MyActivity extends ... implements AlertDialogShow.OnItemChange {
@Override
public void notifyAdapter(int position) {
// Notify your adapter item change here
// e.g.: adapter.notifyItemChanged(position);
}
}
Then you can use OnItemChange
listener in your AlertDialogShow
class like so:
this.listener.notifyAdapter(position);
This will call notifyAdapter(int position)
method in your Activity
and execute code that you have written there.
Good luck.
来源:https://stackoverflow.com/questions/49708825/calling-notifyitemchanged-on-a-recyclerview-adapter-from-another-class