我有一个显示来自数据库的注释的Recycler视图,我在适配器中有一个onclick方法,因此我可以单击每个元素以显示一个对话框来编辑/删除/ make完成,我的问题是,一旦它执行了动作,因为它在适配器中,我如何让它刷新活动的recyclelerview?
适配器类
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();
}
});
}
}
}
活动类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();
答案 0 :(得分:2)
您可以从适配器调用notifyItemInserted / Removed / Changed来告诉RecyclerView究竟发生了什么变化。 有关详细信息,请参阅https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html。
答案 1 :(得分:0)
使你的适配器abstrcat类:
public abstrcat class NoteDataAdapter extends...
并将类抽象方法添加到类中:
public abstract void delete();
public abstract void edit();
单击按钮
时调用这些方法并在您的活动中实施它们