0

I want to have a listview with textview + check box. I was able to create the listview. I can capture listview item select. However when I try to capture check box select, unselect I get a null pointer exception. How to write the setOnCheckedChangeListener() for the check box..

public class LocationActivity extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.locationmain);

    ListView listview = (ListView) findViewById(R.id.listView1);

    String selectQuery = "SELECT  * FROM " +     DatabaseHandler.TABLE_LOCATIONLABLES;
    SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    final List<String> locLables = new ArrayList<String>();

    if(cursor != null){
        if (cursor.moveToFirst()) {
            do {

                locLables.add(cursor.getString(1));

            } while (cursor.moveToNext());
        }
    }

    //String[] locLables = new String[] {"Home","University","Office"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.locationmain_entry,R.id.textView12, locLables);

    //cb gives a null pointer exception
    CheckBox cb = (CheckBox) listview.findViewById(R.id.checkBox12);

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                System.out.println("selected");
            }else if(!isChecked){
                System.out.println("not selected");
            }

        }
    });     
    listview.setAdapter(adapter);


    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();
        }
    });
}
user340
  • 375
  • 12
  • 28
  • 1
    Have a look at these - 1) [listview-with-checkboxes-without-listactivity](http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php) 2) [listview-example-3-simple-multiple-selection-checkboxes](http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/) – Praveenkumar Aug 13 '12 at 04:35

2 Answers2

0

You can maintain seperate adpater class so that you can achive this easily....

 public class bsAdapter extends BaseAdapter
 {
   Activity cntx;
   public bsAdapter(Activity context)
   {
     // TODO Auto-generated constructor stub
     this.cntx=context;

   }

   public int getCount()
   {
     // TODO Auto-generated method stub
     return listview_arr.length;
   }

  public Object getItem(int position)
  {
     // TODO Auto-generated method stub
     return listview_arr[position];
  }

  public long getItemId(int position)
  {
     // TODO Auto-generated method stub
     return listview_array.length;
  }

  public View getView(final int position, View convertView, ViewGroup parent)
  {
     View row=null;

    LayoutInflater inflater=cntx.getLayoutInflater();
    row=inflater.inflate(R.layout.search_list_item, null);

    TextView tv=(TextView)row.findViewById(R.id.title);
    CheckBox cb=(CheckBox)row.findViewById(R.id.cb01);


    tv.setText(listview_arr[position]);

    cb.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {

    if(cb.ischecked)
    {
       //ur code
    }

    else //ur code

    }
  });

  return row;
  }
 }
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

Do not use listview.findViewById(), just use findViewById() like you did for your list.

Unless the checkbox is part of each of the list items, in which case you would have to access the checkbox from within the getView() of your ListAdapter.

dnkoutso
  • 6,041
  • 4
  • 37
  • 58
  • findViewById() id still gives the error. checkbox is part of each list item. can you explain the getView().. (not in a separate adapter) – user340 Aug 13 '12 at 05:45