0

Sorry for my english , i have a question for you ( and thank you for your reply ). I have a recyclerview that componed with a checkbox and three textview. I want create this, when click first checkbox and click last checkbox i want that all range ( 0,1,2,3 ) are checked.

in my fragment i have this method:

 recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for(int i=0; i<3; i++){
                            if(recyclerView.findViewHolderForAdapterPosition(i)!=null )
                            {                              recyclerView.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.linearlayout_item).performClick();
                            }
                        }
                    }
                },50);

and i not have a problem but when i change in this:

if ( posizione_elementi.size() == 2 ) {
                    for (int i = posizione_elementi.get(0); i <= posizione_elementi.get(1); i++) {
                        int finalI = i;
                        recyclerView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                recyclerView.findViewHolderForAdapterPosition(finalI).itemView.findViewById(R.id.linearlayout_item).performClick();
                            }
                        },50);
                    }
                }

I have this error "java.lang.NullPointerException: Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference". Why i have this error ? and how can resolve ?.

Thank you for answered

kuruk
  • 1
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jhamon Mar 09 '23 at 08:32
  • Thank you, but my question is different, because i have this error ( title of topic ) when i execute with for with my parameter and not without. – kuruk Mar 09 '23 at 08:53

1 Answers1

0

i think your purpose cant be achived with the method you are doing it. first in model that you are using for recyclerview item you should have a param for indicate item is checked or not something like this :

itemModel{
 String txt1;
 String txt2;
 Boolean isChecked;

}

and inside of your adapter class have a paramether to keep is a checked item is available for example: boolean checkedItemFlag=false; then in your recyclerview adapter when check status of checkbox of on item is changed , if checkedItemFlag is true you should loop inside your main list and when you reach first checked item, tou change next comming items check status to true until you reach to current item , if checkedItemFlag is false you just change check status of current item to chaecked and set flag status to true finaly you call noifyDataSetChanged method of adapter to apply changes

saber javadi
  • 473
  • 1
  • 7
  • 17