1

Hello I am using ContentProvider and after deleting elements by clicking a button my adapter does not notify, if i go back and come again the element is deleted. So ContentProvider works but notifyDataSetChanged(); doesn't.

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

}

Erald Haka
  • 53
  • 1
  • 8
  • Please provide complete code. Maybe you are using a different list in adapter. – vijaypalod Jan 30 '17 at 09:51
  • i provided complete code in adapter – Erald Haka Jan 30 '17 at 10:05
  • Try updating your onClick method like this. `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); feedItemList.remove(position) notifyDataSetChanged(); } });` – vijaypalod Jan 30 '17 at 10:15
  • 1
    Deleting from ContentProvider is fine, but you did not remove from the AdapterList, before notifyDataSetChange, delete from ArrayList like feedItemList.remove(position). Hope it helps – Ramesh Kumar Jan 30 '17 at 10:20
  • thank u @mvj, u solve my problem – Erald Haka Jan 30 '17 at 10:42
  • thank u @RameshKumar that was the right answer – Erald Haka Jan 30 '17 at 10:46
  • @EraldHaka, I have added this as Answer you can make Accept it as answer, because other can understand this logic. thanks – Ramesh Kumar Jan 30 '17 at 10:57
  • 1
    @RameshKumar done it – Erald Haka Jan 30 '17 at 15:10

4 Answers4

1

I had the same problem like your problem and I solved this problem in this way: In mine, I have an array "arrayBootable" that I fill recycled view with it. You can use your ArrayList name and call this method when you want to notify your recycler

public static void notifyRecycler(){
  arrayBootable.clear();
  arrayBootable.addAll(bootable());
  favouriteAdapter.notifyDataSetChanged();
}
Ehsan
  • 2,676
  • 6
  • 29
  • 56
1

Deleting from ContentProvider is fine, but you did not remove from the AdapterList, before notifyDataSetChange, delete from ArrayList like

feedItemList.remove(position);

Hope it helps.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Ramesh Kumar
  • 1,229
  • 14
  • 24
0

Make sure you are calling notifyChange in the delete method of your content-provider. The way it works is, when the data attached to any view is updated, the notifyChange method notifies that view about the update. So, make sure, you have called getContext().getContentResolver().notifyChange(uri, null); inside the delete method in you CP.

The other reason may be, you are initialising the arraylist/list attached to the recyclerview adapter more than once. In that case, the link to the list is lost from the adapter and you have to setAdapter again. The recommended way is to call clear followed by addAll on the list. like this:

list.clear();
list.addAll(your_new_data);

Let me know if it helps. Thanks!

inkedTechie
  • 684
  • 5
  • 13
  • yes the second answer id the main idea, i have forgot to declare feedItemList.remove(position); thank u for your time – Erald Haka Jan 30 '17 at 10:45
0

Try using notifyDataSetChanged() inside a runnable that posts data on UI thread through a handler.

Define your handler in a Utility method

private static Handler mainHandler = new Handler(Looper.getMainLooper());

public static void postOnUiThread(Runnable runnable) {
        mainHandler.post(runnable);
    }

Then in your code :

Utility.postOnUiThread(new Runnable() {
            @Override
            public void run() {
                // call notifyDataSetChanged here
            }
        });
nipun.birla
  • 719
  • 5
  • 19