我正在使用recyclerview通过游标显示sqlite数据库中的数据,当我从数据库中删除某个项目时,该项目将被删除,但我的recycler视图不会刷新。
我必须回到我的主要活动,然后刷新数据。我已经使用notifyItemRemoved()和notifyDataSetChanged()
这是我的代码。
适配器
public class BookmarkAdapter extends RecyclerView.Adapter<BookmarkAdapter.ViewHolder> {
public Cursor cursor;
DatabaseHelper mDBHelper;
public BookmarkAdapter(){
}
public void setCursor(Cursor cursor){
this.cursor = cursor;
}
@NonNull
@Override
public BookmarkAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.bookmark_item_layout, parent,false);
BookmarkAdapter.ViewHolder viewHolder = new BookmarkAdapter.ViewHolder(v,context);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final BookmarkAdapter.ViewHolder holder, int position) {
cursor.moveToPosition(position);
holder.tvWordBookmark.setText(cursor.getString(1));
holder.btnDeleteBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = cursor.getString(0);
mDBHelper.deleteBookmark(id);
notifyItemRemoved(holder.getAdapterPosition());
notifyDataSetChanged();
}
});
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
if (cursor == null) return 0;
return cursor.getCount();
}
public class ViewHolder extends RecyclerView.ViewHolder {
Context context;
TextView tvWordBookmark;
ImageButton btnDeleteBookmark;
BookmarkAdapter bookmarkAdapter;
public ViewHolder(@NonNull View itemView, final Context context) {
super(itemView);
this.context = context;
mDBHelper = new DatabaseHelper(context);
bookmarkAdapter = new BookmarkAdapter();
tvWordBookmark = itemView.findViewById(R.id.tvWordBookmark);
btnDeleteBookmark = itemView.findViewById(R.id.btnDeleteBookmark);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
cursor.moveToPosition(position);
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("ID",cursor.getString(0));
intent.putExtra("WORD",cursor.getString(1));
intent.putExtra("DEFINITION",cursor.getString(2));
context.startActivity(intent);
}
});
}
}
}
这是我的活动
public class BookmarkActivity extends AppCompatActivity {
private RecyclerView rvWordBookmark;
private BookmarkAdapter bookmarkAdapter;
private DatabaseHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookmark);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
rvWordBookmark = findViewById(R.id.recyclerviewBookmark);
rvWordBookmark.setLayoutManager(new LinearLayoutManager(this));
mDBHelper = new DatabaseHelper(this);
bookmarkAdapter = new BookmarkAdapter();
bookmarkAdapter.setCursor(mDBHelper.getBookmarkWord(""));
rvWordBookmark.setAdapter(bookmarkAdapter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id== android.R.id.home){
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您必须在notifyDataSetChanged();
holder.btnDeleteBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = cursor.getString(0);
mDBHelper.deleteBookmark(id);
//here
notifyItemRemoved(holder.getAdapterPosition());
notifyDataSetChanged();
}
});