gpt4 book ai didi

java - 从适配器中的 onclick 刷新 recyclerview

转载 作者:行者123 更新时间:2023-12-02 03:23:31 25 4
gpt4 key购买 nike

我有一个显示数据库注释的回收器 View ,我在适配器中有一个 onclick 方法,因此我可以单击每个元素以打开一个对话框来编辑/删除/制作完成,我的问题是,一旦它执行了操作,因为它在适配器中,我如何让它刷新 Activity 上的回收 View ?

适配器类

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 fragment

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();

最佳答案

您可以从适配器调用notifyItemInserted/Removed/Changed来告诉RecyclerView到底发生了什么变化。请参阅https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html了解更多详情。

关于java - 从适配器中的 onclick 刷新 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com