データベースからノートを表示するリサイクルビューがあります。アダプタ内にonclickメソッドがありますので、各要素をクリックして編集/削除/削除するダイアログボックスを表示できます。私の問題は、いったんそのアクションを実行したからです。アダプターの中で、アクティビティーのrecyclerviewをリフレッシュするにはどうすればいいのですか?アダプタのonclickからリサイクルビューをリフレッシュ
アダプタクラス
public class NoteDataAdapter extends RecyclerView.Adapter<NoteDataAdapter.RecyclerViewHolder> {
ArrayList<NoteDataProvider> arrayList = new ArrayList<>();
NoteDataAdapter(ArrayList<NoteDataProvider> arrayList)
{
this.arrayList = arrayList;
}
public static String doner;
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_assignment_note_layout,parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
NoteDataProvider noteDataProvider = arrayList.get(position);
holder.id.setText(noteDataProvider.getId());
holder.note.setText(noteDataProvider.getContent());
holder.done.setText(noteDataProvider.getDone());
String doneee = holder.done.getText().toString();
final String yes = "Yes";
doner = holder.done.getText().toString();
if (doneee.equals("Yes"))
{
holder.note.setPaintFlags(holder.note.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else {
// nothing
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView id, note, done;
public NoteDataProvider item;
RecyclerViewHolder(View view){
super(view);
id = (TextView)view.findViewById(R.id.note_id);
note = (TextView)view.findViewById(R.id.note_content);
done = (TextView)view.findViewById(R.id.note_done);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Toast.makeText(v.getContext(), "os version is: " + done.getText().toString()+ " " + doner , Toast.LENGTH_SHORT).show();
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
userDbHelper = new UserDbHelper(v.getContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
userDbHelper.updateNote(id.getText().toString(), "Yes", sqLiteDatabase);
break;
case DialogInterface.BUTTON_NEGATIVE:
//Edit
break;
case DialogInterface.BUTTON_NEUTRAL:
//Delete
userDbHelper = new UserDbHelper(v.getContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
userDbHelper.deleteAssignmentNote(id.getText().toString(), sqLiteDatabase);
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(v.getContext(), R.style.myDialog));
builder.setMessage("What would you like to do?").setPositiveButton("Mark as done", dialogClickListener)
.setNegativeButton("Edit", dialogClickListener).setNeutralButton("Delete",dialogClickListener).show();
}
});
}
}
}
Activityクラスのrecyclerviewスニペット
recyclerView = (RecyclerView)findViewById(R.id.assignment_notes_recyclerview);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new NoteDataAdapter(arrayList);
recyclerView.setAdapter(adapter);
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();
notifydatasetchangedまたはnotifyItemInsertedを使用してください。 –