0

I have created listview using layoutinflator. Listview contains checkbox and textview for each row. I am displaying items in a listview with checkbox checked by default.I can check\uncheck these items.

Now on the same layout I have one button and under clickevent of this button I want to check which item has checkbox in checked state and which has not.

Can anybody have any idea how to check this?

My Inflator code is:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    try {
        mydbadapter.open();
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.syncissue_row, null);
        }
        issue = (TextView) view.findViewById(R.id.issuedescription);
        checkbox = (CheckBox) view.findViewById(R.id.issueCheckBox);
        issue.setText(arrlstFaults.get(position));
        checkbox.setChecked(true);
    } catch (SQLException e) {
        mydbadapter.close();
        e.printStackTrace();
    } finally {
        mydbadapter.close();
    }
    return view;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • look here http://stackoverflow.com/questions/2406937/android-save-checkbox-state-in-listview-with-cursor-adapter – milind Oct 04 '11 at 12:13

1 Answers1

1

My answer is not standard one, but you can go for it if you don't find any other alternative. you can update your getView() method something like bellow.

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    try {
        mydbadapter.open();
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.syncissue_row, null);
        }
        issue = (TextView) view.findViewById(R.id.issuedescription);
        checkbox = (CheckBox) view.findViewById(R.id.issueCheckBox);
        issue.setText(arrlstFaults.get(position));
        // add bellow line
        checkbox.setOnCheckedChangeListener(new MyCheckedChangeListener(position));
        checkbox.setChecked(true);
    } catch (SQLException e) {
        mydbadapter.close();
        e.printStackTrace();
    } finally {
        mydbadapter.close();
    }
    return view;
}

and you can write listener for having event of check changed. something like bellow. you can maintain one Hashtable for storing the check status as shown bellow.

 Hashtable ht = new Hashtable<Integer, Boolean>();

class MyCheckedChangeListener implements OnCheckedChangeListener
{
    int position = -1;
    myCheckedChangeListener(int position){
        this.position = position;
    }

    @Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

   ht.put(position, isChecked);
}

}

I hope It will work for you.

krishna5688
  • 1,130
  • 1
  • 11
  • 19