你好,我正在使用ContentProvider,並通過單擊按鈕刪除元素後,我的適配器不通知,如果我回去再次來元素被刪除。所以ContentProvider的作品,但notifyDataSetChanged();沒有。RecyclerView.Adapter使用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;
}
}
}
請提供完整的代碼。也許你正在使用適配器中的不同列表。 – vijaypalod
我在適配器中提供了完整的代碼 –
嘗試像這樣更新您的onClick方法。 'holder.delete_category.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(查看視圖){ //deleteId(feedItem.getId()+ 「」); 字符串[] whereArgs = { feedItem.getId()+ 「」}; 詮釋計數= mContext.getContentResolver()刪除(UsersProvider.CONTENT_URI,UsersProvider.PID +。 「=」,whereArgs?); feedItemList.remove(位置) notifyDataSetChanged(); } });' – vijaypalod