ssummeraw 2013-04-22 09:07 采纳率: 10%
浏览 2790
已采纳

Listvew 中 textview 和 checkbox 的问题

我想创建一个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();
        }
    });
}
  • 写回答

3条回答 默认 最新

  • yongyong_21 2013-04-23 02:43
    关注

    你可以保持独立的 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;
      }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型