我有一个注释列表,有一个对话框打开以添加新注释,当对话框关闭时,它会保存到数据库中,如何在对话框关闭后刷新recyclelerview显示添加的新笔记。
recyclerView = (RecyclerView)findViewById(R.id.assignment_notes_recyclerview);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
UserDbHelper userDbHelper = new UserDbHelper(this);
SQLiteDatabase sqLiteDatabase = userDbHelper.getReadableDatabase();
Cursor cursor = userDbHelper.getAssignmentNotes( selected, sqLiteDatabase);
cursor.moveToFirst();
do {
NoteDataProvider noteDataProvider = new NoteDataProvider(cursor.getString(0), cursor.getString(2), cursor.getString(3));
arrayList.add(noteDataProvider);
}while(cursor.moveToNext());
userDbHelper.close();
adapter = new NoteDataAdapter(arrayList);
recyclerView.setAdapter(adapter);
assignmentAddNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.layout_assignment_note_dialog, null);
dialogBuilder.setView(dialogView);
final EditText note = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Assignment Notes");
dialogBuilder.setMessage("Enter te note below");
dialogBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//save button has been pressed
String done = "No";
String content = note.getText().toString();
userDbHelper1 = new UserDbHelper(context);
sqLiteDatabase1 = userDbHelper1.getReadableDatabase();
userDbHelper1.addAssignmentNote(selected, content, done, sqLiteDatabase1);
}
});
dialogBuilder.setNegativeButton("Discard", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
});
答案 0 :(得分:3)
要刷新 RecyclerView ,请将新元素添加到ArrayList
类正在使用的RecyclerViewAdapter
,在您的情况下为 arrayList
arrayList.add(<Your item here >);
然后刷新,
adapter.notifyDataSetChanged();