0

I have an activity that has a checkbox, then under it is a list of contacts populating my listview with checkboxes for each contact. I have two major problems stemming from my question.

  1. chkbox_foo is outside of the listview, and chk_bar is inside. chk_foo works, but anything related to chk_bar after being initialized causes the app to crash. Also, if I create a setOnCheckedChangeListener for chkbox_bar, that will cause the app to crash also. Does anyone know why this is happening and how I can fix this?

    btn_foo = (Button) findViewById(R.id.btn_foo);
    barList = (ListView) findViewById(R.id.lv_barList);
    chk_foo = (CheckBox) findViewById(R.id.cb_foo);
    chk_bar = (CheckBox) findViewById(R.id.cb_bar);
    
    // set checkboxes state as false at beginning
    chkboxAllVisible = false;
    chkboxSingleChk = false;
    
    chk_foo.setChecked(chkboxAllVisible);
    chk_bar.setChecked(chkboxChk);  <---App crashes here
    
    // Outside of listview checkbox 
    chk_foo.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            Log.d(TAG, "checkbox changed " + isChecked);
            if(isChecked){
                chkboxAllVisible = true;
                chk_bar.setChecked(isChecked); <---app crashes here too
            } 
        }
    });
     // Outside of listview checkbox 
    chk_bar.setOnCheckedChangeListen... <---app crashes here also
    

2 Answers2

1

When the user clicks on your "master" checkbox you will need to iterate through the list that is bound to your ListView. Then by accessing each row individually can you mark each row's checkbox.

Please post the code for your ListView if you want a specific example.

Sam
  • 86,580
  • 20
  • 181
  • 179
0

chk_bar is probably null. You can't call findViewByID on the main layout if the checkbox is inside a listview. You have to call it on the layout where it is housed/contained, which would be the listview item.

That said, you don't need to do that. If you have contacts with checkboxes, I'm guessing you are using some sort of custom adapter? If so, good, then it this will be easy. I'm also assuming you are keeping track of whether or not an item/contact has been checked (if not, you'll need to implement that anyways, see tutorial below)

In your getView of your custom adapter, while generating the view, you need to look at the data object and if the IsChecked value is true, then you check the checkbox.

public View getView(int position, View convertView, ViewGroup parent)
{
    //Inflate your view
    //View listViewLayout = ...

    //Here is where you get the reference to the checkbox
    Checkbox chkBox = (CheckBox)listViewLayout.findViewById(R.id.cb_bar);

    //Get the data object from whatever your source is (maybe an array list?)
    SomeObj curObj = (SomeObj)listOfItems.get(position);

    chkBox.setChecked(curObj.IsChecked);
}

Here is a full tutorial on how to implement a custom Adapter for Listview: http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

Once you have that working, the rest is simple:

When the user clicks the master checkbox:

  1. Regenerate the list of items but force all the checkbox values to true.
  2. Call adapter.notifyDataSetChanged()

When you call notifyDataSetChanged() the listview will re-draw itself (if it believes the source items have in fact been changed. See this: notifyDataSetChanged example)

When it regenerates the listview, it will iterate through your source items you passed in, and since you regenerated the list and marked all items as "IsChecked" (or whatever your variable name is), it will check each checkbox.

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14
  • Aaaah, I see what you are saying. Thank you for your input! I my checkbox for the list in the wrong place! –  May 10 '12 at 03:32