如何給checkbox創建setOnCheckedChangeListener()?

原問題來自於CSDN問答頻道,更多解決方案見:http://ask.csdn.net/questions/2185

問題描述:

我想創建一個listview,包含textview 和 checkbox。我創建好了 listview,也可以捕捉listview item select,但是當我想捕捉 checkbox 的select和unselect時,獲得null pointer exception異常。如何給checkbox創建setOnCheckedChangeListener()?

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();
        }
    });
}


解決方案:

你可以保持獨立的 adpater 類,那樣就可以實現

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;
  }
 }


 

發佈了95 篇原創文章 · 獲贊 125 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章