您好我正在使用ContentProvider,并且在通过单击按钮删除元素后,我的适配器不会通知,如果我返回并再来一次,则元素将被删除。所以ContentProvider工作但notifyDataSetChanged();没有。
public class CategoriesPreferedAdapter extends RecyclerView.Adapter<CategoriesPreferedAdapter.ViewHolder> {
private List<ModelCategoriesProvider> feedItemList;
private Context mContext;
public CategoriesPreferedAdapter(Context context, List<ModelCategoriesProvider> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public CategoriesPreferedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.categories_prefered_adapter, parent, false);
return new CategoriesPreferedAdapter.ViewHolder(itemView);
}
@Override
public void onBindViewHolder(CategoriesPreferedAdapter.ViewHolder holder, final int position) {
//holder.titleTextView.setText(mList[position]);
final ModelCategoriesProvider feedItem = feedItemList.get(position);
holder.setClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(view.getContext(),""+feedItem.getId()+" Users Delete",Toast.LENGTH_LONG).show();
if (isLongClick) {
}
}
});
holder.title.setText(feedItem.getName());
Glide.with(mContext)
.load(Links.ImagePath+feedItem.getImage())
.centerCrop()
// .placeholder(R.drawable.ic_porosiicon)
.crossFade()
.into(holder.categoryImage);
System.out.println("image path "+feedItem.getImage());
holder.delete_category.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//deleteId(feedItem.getId()+"");
String[] whereArgs = {feedItem.getId()+""};
int count = mContext.getContentResolver().delete(UsersProvider.CONTENT_URI,UsersProvider.PID+" =?",whereArgs);
// System.out.println("count erald mjeshtri + mbreti ndaj thuaj princ ndaj thuaj mbret: "+count);
//EventBus.getDefault().post(new Db_Update("update"));
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener{
private TextView title,data,type,amount,billnr;
private ImageView categoryImage,delete_category;
private ItemClickListener clickListener;
CardView card_view;
public ViewHolder(View itemView) {
super(itemView);
card_view = (CardView) itemView.findViewById(R.id.card_view);
title = (TextView) itemView.findViewById(R.id.repoName);
categoryImage = (ImageView) itemView.findViewById(R.id.imageView_category);
delete_category = (ImageView) itemView.findViewById(R.id.imageViewfavourites);
itemView.setTag(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
@Override
public void onClick(View view) {
clickListener.onClick(view, getPosition(), false);
}
@Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
}
答案 0 :(得分:1)
我遇到了与你的问题相同的问题,我用这种方式解决了这个问题: 在我的,我有一个数组“arrayBootable”,我用它填充回收的视图。 您可以使用ArrayList名称,并在想要通知回收站时调用此方法
public static void notifyRecycler(){
arrayBootable.clear();
arrayBootable.addAll(bootable());
favouriteAdapter.notifyDataSetChanged();
}
答案 1 :(得分:1)
从ContentProvider
删除是免费的,但您在AdapterList
之前没有从notifyDataSetChange
删除,请从ArrayList
删除
feedItemList.remove(position);
希望它有所帮助。
答案 2 :(得分:0)
确保在内容提供商的删除方法中调用notifyChange。它的工作方式是,当更新附加到任何视图的数据时,notifyChange方法会通知该视图有关更新的信息。因此,请确保您在CP中的delete方法中调用了getContext().getContentResolver().notifyChange(uri, null);
。
另一个原因可能是,您正在多次初始化附加到recyclerview适配器的arraylist / list。在这种情况下,列表的链接将从适配器中丢失,您必须再次setAdapter
。建议的方法是在列表中调用clear
,然后调用addAll
。
像这样:
list.clear();
list.addAll(your_new_data);
如果有帮助,请告诉我。 谢谢!
答案 3 :(得分:0)
尝试在runnable中使用notifyDataSetChanged(),通过处理程序在UI线程上发布数据。
在Utility方法中定义处理程序
private static Handler mainHandler = new Handler(Looper.getMainLooper());
public static void postOnUiThread(Runnable runnable) {
mainHandler.post(runnable);
}
然后在你的代码中:
Utility.postOnUiThread(new Runnable() {
@Override
public void run() {
// call notifyDataSetChanged here
}
});