0

I am using PagedList live data and PositionalDataSource to create information in mvvm pattern. I tried all possible solutions on stackoverflow similar to it . It didn't work.

on Below code i have null pointer exception : Attempt to invoke virtual method '..Item.getSelected()' on a null object reference

I have very carefully set it on Item class .

@Override
public void onBindViewHolder(PageViewAdapter.PageViewHolder holder, int position) {

    item = getItem(position);
    if (item != null) {
        holder.lineTextView.setText(String.valueOf(item));
    }

    holder.checkBox.setTag(position);
    holder.checkBox.setOnCheckedChangeListener(null);
    holder.checkBox.setChecked(item.getSelected()); 
    holder.checkBox.setOnCheckedChangeListener((compoundButton, isChecked) -> {

        if (compoundButton.isPressed()) {
         // holder.checkBox.setSelected(isChecked);
            item.setSelected(isChecked);
        }
    });
}

My Data Model as Item Class 
public class Item {
 private boolean selected = true;

public Item(String value) {
    this.value = value;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public boolean getSelected() {
    return selected;

}
}

my code on Git : https://github.com/kumarjitendra/pageViewItem

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
jitendra kumar
  • 2,161
  • 2
  • 7
  • 12

2 Answers2

0

RecyclerView means that views are recycled. When they disappear above your screen they get reused at the bottom of your screen. That is why some of them are still checked and it will appear "random". You'll have to check for that when you bind to the view in your Recycler View Adapter.

You may probably find your answer here: CheckBox in RecyclerView keeps on checking different items

Twometer
  • 1,561
  • 2
  • 16
  • 24
  • I tried all the possible option from the link . none of them worked in my case .at one place they suggested to use setSelected and getSelected . in this case i am getting null pointer exception at "getSelected()" though i have set it very carefully Thanks – jitendra kumar Jan 15 '18 at 10:23
  • on the place where i defined Recyclerview, i have added below 2 line recylerView.setHasFixedSize(true); recylerView.setItemViewCacheSize(X); X = Numbers of max rows which will be cached when scrolling. it's working fine . Thanks everyone for time and effort :) – jitendra kumar Jan 15 '18 at 11:20
0

Because RecyclerView recycles views, you need to save some info that identifies the previously selected items. You can use the data model to save that info and then on adapter check.

dledo
  • 44
  • 3
  • I have already used datamodel with name "Item" . I have used setSelected and getSelected() very carefully still , i am getting null pointer exception at "getSelected()" when i use setChecked my code : holder.checkBox.setChecked(item.getSelected()); holder.checkBox.setOnCheckedChangeListener((compoundButton, isChecked) -> { if (compoundButton.isPressed()) { // holder.checkBox.setSelected(isChecked); item.setSelected(isChecked); } }); – jitendra kumar Jan 15 '18 at 10:30
  • on the place where i defined Recyclerview, i have added below 2 line recylerView.setHasFixedSize(true); recylerView.setItemViewCacheSize(X); X = Numbers of max rows which will be cached when scrolling. it's working fine . Thanks everyone for time and effort :) – jitendra kumar Jan 15 '18 at 11:20