binu j
binu j

Reputation: 399

recyclerView ClickListener return null after notifyDataSetChanged()

i am using a recyclerView and loading the data from database.The recyclerView item have 2 buttons. The recyclerView loads fine and clicks work fine for the following code.

allData=db.getCatData(CATEGORYLIST_CIDD,1);
textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);

later i rearrange the adapter data for displaying the data in descending order by using this code

    db=new DatabaseHandler(getActivity().getApplicationContext());
    allData=db.getCatData(CATEGORYLIST_CIDD,order_id);
    textlistadapter = new TextListAdapter(getActivity(), allData);
    recyclerView.setAdapter(textlistadapter);
    textlistadapter.notifyDataSetChanged();

Now the items are rearranged on the recyclerView, but the onShareButtonClickListener returns null, here is the code of the adapter.

private OnShareButtonClickListener onShareButtonClickListener;
public interface OnShareButtonClickListener {
    void onItemClick(View view, Pojo obj, int position);
}
public void setOnShareButtonClickListener(final OnShareButtonClickListener onShareButtonClickListener) {
    this.onShareButtonClickListener = onShareButtonClickListener;
}

the click listener code is here

 holder.bt_share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Snackbar.make(view, "Share Clicked", Snackbar.LENGTH_SHORT).show();
            if (onShareButtonClickListener != null) {
                onShareButtonClickListener.onItemClick(view, c, position);
            }
        }
    });

The click does not work since the onShareButtonClickListener is null.

The same code works well for the first time and once i change the order then the click events are not working, all other functions are working well.

Upvotes: 2

Views: 358

Answers (2)

TWL
TWL

Reputation: 6664

you should only need to do this once:

textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);

later on, if you want to update/change the data, then you should do something like this:

textlistadapter.setListItems(allData);
textlistadapter.notifyDataSetChanged();

Where setListItems() is a public method in your adapter, an example like this:

public void setListItems(List<> allData) {
    this.data.clear();
    this.data.addAll(allData);
}

Upvotes: 2

Ricardo
Ricardo

Reputation: 9696

It's easy to understand what you are doing wrong. You populate an array which will feed your adapter with data, that meaning that, after setting the adapter, if you want it's data to change, you only need to change the array you populated the data with, and notify the adapter for the data change.

For example:

myData = db.getData();
myAdapter = new MyAdapter(this,myData);
recyvlerview.setAdapter(myAdapter);

Now every time you want to change the data, you only need to change your myData and notifity the adapter

myAdapter.notifyDataSetChange();

Basically instead of doing this:

db=new DatabaseHandler(getActivity().getApplicationContext());
allData=db.getCatData(CATEGORYLIST_CIDD,order_id);
textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);
textlistadapter.notifyDataSetChanged();

This should suffice

db=new DatabaseHandler(getActivity().getApplicationContext());
allData=db.getCatData(CATEGORYLIST_CIDD,order_id);
textlistadapter.notifyDataSetChanged();

Upvotes: 1

Related Questions