我正在开发新闻应用程序。在此应用程序中,用户可以存储喜爱的新闻。该新闻存储在SQLite数据库中。当用户单击recyclerView图像时,将显示一个自定义对话框,其中包括3个按钮。
一个按钮用于使用浏览器读取存储。它将使用存储在SQLite中的URL打开浏览器。第二个按钮用于删除。当用户单击“删除”按钮时,该方法将被调用并删除数据,但不会更新recyclerView数据。
您需要重新打开片段以更新recyclerView数据。我本可以使用notifyDataChange,但是所有这些都在Adapter类中发生。片段加载后,它从SQLite获取数据,并使用setter将数据发送到模型类。然后将该对象添加到该模型的列表中,最后将其发送到适配器类。
片段:
private void getDataFromSQLite() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_NAME;
Cursor cursor = db.rawQuery(selectQuery,null);
if (cursor.moveToFirst()) {
do {
NewsModel newsModel = new NewsModel();
newsModel.setSourceName(cursor.getString(1));
newsModel.setAuthor(cursor.getString(2));
newsModel.setTitle(cursor.getString(3));
newsModel.setDescription(cursor.getString(4));
newsModel.setUrl(cursor.getString(5));
newsModel.setUrlToImage(cursor.getString(6));
newsModel.setPublishedAt(cursor.getString(7));
newsModelList.add(newsModel);
} while (cursor.moveToNext());
}
setRvadapter(newsModelList);
cursor.close();
}
public void setRvadapter (List<NewsModel> lst) {
myAdapter = new RvAdapterFavourite(getActivity(),lst) ;
myrv.setLayoutManager(new LinearLayoutManager(getActivity()));
myrv.setAdapter(myAdapter);
}
适配器类:
public class RvAdapterFavourite extends RecyclerView.Adapter<RvAdapterFavourite.MyViewHolder> {
private RequestOptions options ;
private Context mContext ;
private List<NewsModel> mData ;
private DatabaseHelper databaseHelper;
public RvAdapterFavourite(Context mContext, List<NewsModel> list) {
this.mContext = mContext;
this.mData = list;
options = new RequestOptions()
.centerCrop()
.placeholder(R.drawable.ic_load)
.error(R.drawable.loading_shape);
databaseHelper = new DatabaseHelper(mContext);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.news_item_row_favourite, parent, false);
// click listener here
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.tvAuthor.setText(String.format("Author: %s", mData.get(position).getAuthor()));
holder.tvSourceName.setText(String.format("Source: %s", mData.get(position).getSourceName()));
holder.tvPublishedAt.setText(String.format("Published At: %s", mData.get(position).getPublishedAt()));
holder.tvTitle.setText(mData.get(position).getTitle());
holder.tvDescription.setText(mData.get(position).getDescription());
//set listener on pic to read full story in browser
holder.newsThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showOptionsDialog(mData.get(holder.getAdapterPosition()).getUrl());
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvAuthor,tvSourceName,tvPublishedAt,tvTitle,tvDescription;
ImageView newsThumbnail;
MyViewHolder(View itemView) {
super(itemView);
tvAuthor = itemView.findViewById(R.id.author);
tvSourceName = itemView.findViewById(R.id.sourceName);
tvPublishedAt = itemView.findViewById(R.id.publishedAt);
tvTitle = itemView.findViewById(R.id.newsTitle);
tvDescription = itemView.findViewById(R.id.newsDescription);
newsThumbnail = itemView.findViewById(R.id.NewsImage);
}
}
//show dialog box on click
@SuppressLint("SetTextI18n")
private void showOptionsDialog(final String linkToStory)
{
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
databaseHelper.deleteFromData(linkToStory);
dialog.dismiss();
}
});
dialog.show();
}
}
答案 0 :(得分:0)
如@Taseer Ahmad的评论中所建议,我已经更新了onDelete代码
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mData.remove(position);
notifyItemChanged(position);
databaseHelper.deleteFromData(linkToStory);
dialog.dismiss();
}
});