gpt4 book ai didi

android - Recyclerview 不更新数据列表

转载 作者:行者123 更新时间:2023-11-30 05:04:28 25 4
gpt4 key购买 nike

我正在使用 recyclerview 使用游标显示来自 sqlite 数据库的数据,当我从数据库中删除一个项目时,该项目被删除但我的回收站 View 没有刷新。

我必须回到我的主要 Activity ,然后数据会被刷新。我使用了 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);

}
});
}
}
}

这是我的 Activity

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

最佳答案

您必须在 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();

}
});

关于android - Recyclerview 不更新数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54805332/

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