Good day! I'm trying to write an application for Android, and I have the following problem. I have a ListView that is filled with items. Each item has a checkbox. I want to click on the button to check all checkboxes. I try to do it like this:

ListView list = (ListView)findViewById(R.id.myList); for(int i = 0; i < list.getCount(); i++) { View view = ((CheckBox)list.getChildAt(i); view.findViewById(R.id.myCheckbox)).setChecked(true); } 

There are a total of seven elements in the ListView, the screen fits 4. So, when i becomes more than 3, then when I try to access list.getChildAt(i) I get a NullPointerException . As far as I understand, with the help of getChildAt I can refer only to those elements that are visible on the screen. And how can I refer to all the elements in the ListView in general, regardless of whether it is displayed on the screen or not? Is it possible to somehow get all the 'tktvtyns ListView? Thank you in advance!

  • Did not quite understand you. See it. Suppose I have a list of some models that I want to display in a ListView. I bind them to the ListView, they are displayed on the screen, but not all fit, and need to scroll. So far everything is great. But in each ListView element there is a CheckBox element. I also have a button. I want, when I press the button, all checkboxes are checked. When I press the second time I press - so that the jackdaw is removed. To do this, I try to sort through all the elements in the ListView and find the checkbox in them. But it turns out to refer only to the first four elements. What I'm doing wrong - JuniorTwo
  • I did not read anything, but I see by the code - crap, the first one, because, besides setting the flag, I need to update the value. - Gorets

4 answers 4

Let's go on the other hand, if you have checkboxes, most likely you have a custom adapter, then - in it, hang a listener on the checkbox, which will change the state of the object, then on the DOWN button ALL hang up the action, which will run through all your objects and give them the flag of the selected checkbox, and then the adapter to make a notification of the Data Date Set Chendzh.

If everything that is written is not clear, learn the materiel. =)

    Filled on githab: https://github.com/JenyaKirmiza/TestListView

    I did on stupid.

    Here is a custom adapter:

     import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import org.w3c.dom.Text; import java.util.ArrayList; import java.util.HashMap; import java.util.zip.CheckedInputStream; public class ListAdapter extends BaseAdapter{ ArrayList< Integer> items; private LayoutInflater inflater = null; private ViewHolder viewHolder; private Integer wp; private Context c; public void setSelectedAll() { this.isSelectedAll = !isSelectedAll; } /*--- a simple View Holder class ---*/ static class ViewHolder { public CheckBox checkBox; public TextView textView; } private boolean isSelectedAll=false; /*--- Context and all weapons of specified class are passed here ---*/ public ListAdapter(ArrayList<Integer> items, Context c) { this.items = items; inflater = LayoutInflater.from(c); this.c = c; } @Override public int getCount() { return items.size(); } @Override public Integer getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { /*--- initialize our Weapon Object ---*/ wp = items.get(position); if (convertView == null) { /*--- no View is available. Inflate our list item layout and init the Views we need ---*/ convertView = inflater.inflate(R.layout.list_row, null); viewHolder = new ViewHolder(); viewHolder.checkBox = (CheckBox) convertView .findViewById(R.id.checkBox); viewHolder.textView = (TextView) convertView .findViewById(R.id.textView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } if (isSelectedAll) viewHolder.checkBox.setChecked(true); else viewHolder.checkBox.setChecked(false); viewHolder.textView.setText(wp+""); return convertView; } } 

    Here is the activity:

     import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MyActivity extends Activity { private ListView listView; private ListAdapter listAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); listView=(ListView)findViewById(R.id.listView); ArrayList<Integer> arrayList=new ArrayList<Integer>(); for(int i=0;i<100;i++) arrayList.add(i); listAdapter=new ListAdapter(arrayList, getApplicationContext()); listView.setAdapter(listAdapter); Button btn=(Button)findViewById(R.id.btnSelectAll); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listAdapter.setSelectedAll(); listAdapter.notifyDataSetChanged(); } }); } } 

    This is so fast. In general, I would do as Gorets advised. The listener hung on the checkbox in getView, and the listener created it in the custom adapter.

    Pay attention to the use of the ViewHolder pattern. I also advise you to load all images in a sheet of views in the background.

      It is necessary to receive them through the adapter.

       list.getAdapter().getItem(position); 
      • So I will get my model tied to an item in ListViiew. I would like to get the item itself from the ListView. - JuniorTwo

      So they are created and exactly as much as fit on the screen. In the future, already created View simply reused.

      And what you want to do is implemented by setting the desired selection mode of the ListView :

       listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); 

      Followed by a call

       listView.setItemChecked(itemPosition, true); 

      And yes, in order for all this magic to work correctly, do not forget to implement the Checkable interface for the View , which is a ListView element.

      • I probably did not understand something from your answer, but the fact is that I want to mark not the View itself, but the CheckBox that is in it. And I want to do this not only with my hands (it is marked manually), but also programmatically. That is, by pressing the "select all" button. - JuniorTwo
      • So no problem. Tick ​​off what you need. To do this, you need to implement Checkable . In general, read about optimization used in ListView . And even better, and even easier - see the source code AdapterView . Much will become clearer. - falstaf
      • one
        I probably ask some stupid questions, but I don’t understand anything at all. I was advised above to use list.getAdapter (). GetItem (position) and it almost works. I get all the elements, I can find the checkbox in them. But then for some reason I can not mark this checkbox. That is, I do this: for (int i = 0; i <list.getCount (); i ++) {View item = adapter.getView (1, null, list); CheckBox ch = ((CheckBox) item.findViewById (R.id.myCheckbox)); ch.setChecked (true); } The code runs without errors. But the checkboxes themselves are not marked and remain unchecked. - JuniorTwo
      • Because, with the above code, you do not receive existing elements, but create a new View , which you will not use anywhere else. In general, follow my advice and read the documentation, there everything is very detailed chewed. And look at the sorta. And do not invent anything. - falstaf
      • one
        I see you, despite the lack of understanding, stubbornly do not want to read the documentation. I'm afraid, in that case, I can't help you anymore. - falstaf