How to check a box from a ListView Android server?

I get a checkbox from OnItemClickListener using the snippet below

onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.w("TAG","onItemClick clicked position :"+position);

        CheckBox cbx = (CheckBox)view.findViewById(R.id.c_checkbox);
        if(cbx.isChecked()){
            Toast.makeText(getApplicationContext(), 
                    "Checked position " + shoppingList.get(position).getItem(), 
                    Toast.LENGTH_SHORT).show();
        }

I need to get all the items to check which list item is checked. For this, I used the fragment below

int firstPosition = list.getFirstVisiblePosition();
             for(int i=firstPosition;i<=list.getCount();i++){
             View v=list.getChildAt(i);
             cbx = (CheckBox)v.findViewById(R.id.c_checkbox);
             if(cbx.isChecked()){
            }
         }

in the cbx code below giving me a null exception.i pointer used the ViewHolder for the user adapter. Please give me a solution. Why does the same thing work in OnItemClickListener?

Regards, Rajendar

+3
source share
1 answer

he thinks it might be

for(int i=firstPosition;i< **=** list.getCount();i++)

but this is just a wild hunch.

why not use a verifiable listview and then use this ?

+5
source

Source: https://habr.com/ru/post/1780184/


All Articles