用的TextView和复选框Listvew [英] Listvew with textview and checkbox
本文介绍了用的TextView和复选框Listvew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想与TextView的+复选框列表视图。我能够创建列表视图。我可以捕获ListView项选择。然而,当我试图捕捉复选框选择,取消选择,我得到一个空指针异常。怎样写setOnCheckedChangeListener()的复选框。
公共类LocationActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState); 的setContentView(R.layout.locationmain); ListView控件列表视图=(ListView控件)findViewById(R.id.listView1); 字符串selectQuery =SELECT * FROM+ DatabaseHandler.TABLE_LOCATIONLABLES;
SQLiteDatabase DB =新的数据库处理器(本).getReadableDatabase();
光标光标= db.rawQuery(selectQuery,NULL); 最终名单<串GT; locLables =新的ArrayList<串GT;(); 如果(指针!= NULL){
如果(cursor.moveToFirst()){
做{ locLables.add(cursor.getString(1)); }而(cursor.moveToNext());
}
} //的String [] = locLables新的String [] {家,大学,办公室};
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,R.layout.locationmain_entry,R.id.textView12,locLables); // CB给出了一个空指针异常
复选框CB =(复选框)listview.findViewById(R.id.checkBox12); cb.setOnCheckedChangeListener(新OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
// TODO自动生成方法存根
如果(器isChecked){
的System.out.println(选择);
}否则如果(!器isChecked){
的System.out.println(未选中);
} }
});
listview.setAdapter(适配器);
listview.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
// TODO自动生成方法存根
Toast.makeText(getApplicationContext(),locLables.get(ARG2),Toast.LENGTH_SHORT).show();
}
});
}
解决方案
您可以保持独立的adpater类,这样就可以很容易地achive这个....
公共类bsAdapter延伸BaseAdapter
{
活动CNTX;
公共bsAdapter(Activity上下文)
{
// TODO自动生成构造函数存根
this.cntx =背景; } 公众诠释getCount将()
{
// TODO自动生成方法存根
返回listview_arr.length;
} 公共对象的getItem(INT位置)
{
// TODO自动生成方法存根
返回listview_arr [位置]
} 众长getItemId(INT位置)
{
// TODO自动生成方法存根
返回listview_array.length;
} 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
查看排= NULL; LayoutInflater吹气= cntx.getLayoutInflater();
行= inflater.inflate(R.layout.search_list_item,NULL); TextView的电视=(TextView中)row.findViewById(R.id.title);
复选框CB =(复选框)row.findViewById(R.id.cb01);
tv.setText(listview_arr [位置]); cb.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ 如果(cb.ischecked)
{
//乌尔code
} 否则//乌尔code }
}); 返回行;
}
}
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();
}
});
}
解决方案
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;
}
}
这篇关于用的TextView和复选框Listvew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文