如何在ListView中添加CheckBox

yxmsw2007 2011-03-22 10:01:43
我自定义了一个ListView布局,想在里面添加一个CheckBox按钮,我遇到了下面的第二个问题,虽然有解决方法但不知道具体怎么做,最好能给个完整的例子,谢谢大家了!
1、ListView item中加入checkbox后onListItemClick 事件无法触发。
原因:checkbox的优先级高于ListItem于是屏蔽了ListItem的单击事件。
解决方案:设置checkbox的android:focusable="false"
2、选择其中的checkbox,当滚动ListView的时候,会出现一些Checkbox选择错位的现象,
原因:为记住Checkbox的选择状态
解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置

...全文
1119 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
90worker 2011-12-01
  • 打赏
  • 举报
回复
有例子给我也发一份272570596@qq.com
yxmsw2007 2011-03-23
  • 打赏
  • 举报
回复
4L没明白我的意思,我遇到的问题是这样的,比如我要群发一条短信,群发对象都从电话本中选择,这就需要用一个List将联系人都显示,当我们选择对象的时候就需要用Checkbox,但是我做出来后出现这样一个问题,假如我有30个联系人,我手机一次只能显示10条记录,当我选中第一个联系人的时候,会连带将第11、21这两个人也选上;下面是我在网上找的解决方法,但我没看明白,请大家帮忙看下。
选择其中的checkbox,当滚动ListView的时候,会出现一些Checkbox选择错位的现象,
原因:为记住Checkbox的选择状态
ListView中的getChildCount()并不总是等于ListAdapter中的数据行数。当手机一屏显示不了所有数据时(需要翻页),getChildCount()就等于手机一屏幕所显示的行数,小于ListAdapter中的数据行数。而ListView的getCount()是与ListAdapter中的数据行数相同。
当光标下移到屏幕最底部,新显示出来的View,最初的3个(不知道为什么是3个),在Adapter中调用getView(int position, View convertView, ViewGroup parent)方法中,会判断convertView为null,而再有新的View就会发现convertView不为空,所以新显示的View其实使用了之前某个View的对象。这就造成了状态可能混乱。比如第一行的checkbox点选时,第11行的也同时会被点选。
解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置
代码如下:
Java代码
Object b = (Object ) getItem(position);
if (b != null) {
if(selectedSet.contains(b)){
viewHolder.checkBox.setChecked(true);
}else{
viewHolder.checkBox.setChecked(false);
}
}
foley_liao 2011-03-23
  • 打赏
  • 举报
回复
我遇到过这个问题
问题1很好解决:设置 checkbox 为:android:focusable="false 就行
<CheckBox android:id="@+id/list_size"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false" android:clickable="false" />

问题2 就不太好说,你可以用一个对象保存每一个listitem的信息,其中就可以保存一项 ischecked
在你的adapter的getview中每次读取的时候设置保存的ischecked 。在listitem的单击事件中也要保存ischecked(如果是通过单击改变checkbox的状态的话) 不知道说明白了没有。
oDon 2011-03-23
  • 打赏
  • 举报
回复
checkbox Click事件 放到 adapter中响应
kkkk11kk 2011-03-23
  • 打赏
  • 举报
回复
汗····本来都是弄好板式的 结果贴进来就乱了。。。。。
kkkk11kk 2011-03-23
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView

android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<CheckBox
android:text="选择一"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>
<CheckBox
android:text="选择二"
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>

</LinearLayout>
这是xml文件 希望对楼主有用
qbwjly 2011-03-23
  • 打赏
  • 举报
回复
自定义adapter试一试
kkkk11kk 2011-03-23
  • 打赏
  • 举报
回复
这是我自己实现的一个自定义ListView 点击选择一会弹出一个dialog


public class checkBtnView extends ListActivity {
private List<Map<String,Object>> hashMap;
//CheckBox box1 = (CheckBox)findViewById(R.id.checkBox1);
//CheckBox box2 = (CheckBox)findViewById(R.id.checkBox2);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hashMap = getData();
Log.i("debug", "start debuging");
MyAdapter adapter = new MyAdapter(this);
Log.i("debug", "MyAdapter successed");
setListAdapter(adapter);
Log.i("debug", "setListAdapter successed");
}

private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "NO.1");
//map.put("text", "No.2");
list.add(map);
// TODO Auto-generated method stub
return list;
}

public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("nice")
.setMessage("good day")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub

}
}).show();


}

public final class ViewHolder{
public TextView title;
public CheckBox box1;
public CheckBox box2;
}

public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount()
{
// TODO Auto-generated method stub
// this is some debug;
return hashMap.size();
}

@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}




@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder = null;
if (holder == null)
{
Log.i("MyAdapter debug", "start");
holder = new ViewHolder();
Log.i("MyAdapter", "set convertView");
convertView = mInflater.inflate(R.layout.main, null);
holder.title = (TextView)convertView.findViewById(R.id.textView1);
holder.box1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
holder.box2 = (CheckBox)convertView.findViewById(R.id.checkBox2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
Log.i("MyAdapter", "run wrong");
}


Log.i("MyAdapter", "setClick");
holder.title.setText((String)hashMap.get(position).get("title"));
Log.i("MyAdapter", "TX normal");
holder.box1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
showInfo();

}
});
return convertView;
}

}

}
小裴同学 2011-03-23
  • 打赏
  • 举报
回复
1.你都给出结果了,还问啥
2.在getview中加入checkbox 的事件,在把checkbox的状态存到数据库中最好的方法

以下代码仅供参考
package com.jftt.dice.activity;

import com.jftt.dice.date.DataBase;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ModelActivity extends Activity {
private ListView listview;
private DataBase mDateBase;
private Cursor mCursor;
private String modelname;
private String strOpt;
private ImageButton ImageButton01;
private String one;
private String two;
private String three;
private String four;
private String five;
private String six;
private String time;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
strOpt = dm.widthPixels + "*" + dm.heightPixels;
if (strOpt.equals("480*800") || strOpt.equals("600*1024")) {
setContentView(R.layout.main);
}
if (strOpt.equals("320*480")) {
setContentView(R.layout.mainone);
}
ImageButton01 = (ImageButton) findViewById(R.id.ImageButton01);
ImageButton01.setOnClickListener(new Listener());
mDateBase = new DataBase(this);
mCursor = mDateBase.select();// 查询数据库
startManagingCursor(mCursor);
if (strOpt.equals("480*800") || strOpt.equals("600*1024")) {
listview = (ListView) findViewById(R.layout.user);
}
if (strOpt.equals("320*480")) {
listview = (ListView) findViewById(R.layout.userone);
}
listview.setAdapter(new MyAdapter(this));// 加入适配器
listview.setOnItemClickListener(new ListListrner());
}

class ListListrner implements OnItemClickListener {
private boolean c = true;

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 <= 7) {
c = false;
}
mCursor.moveToPosition(arg2);
modelname = mCursor.getString(1);
AlertDialog.Builder dialogBuilder = new Builder(ModelActivity.this);
dialogBuilder.setTitle(modelname);
String[] strarrStrings = new String[] {
getString(R.string.add_title),
getString(R.string.model_change),
getString(R.string.model_delete), getString(R.string.no) };
dialogBuilder.setItems(strarrStrings,
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intent = new Intent();
intent.setClass(ModelActivity.this,
AddActivity.class);
startActivity(intent);
break;
case 1:
if (c == false) {
Toast.makeText(ModelActivity.this,
getString(R.string.toast_nochange),
Toast.LENGTH_SHORT).show();
break;
}
modelname = mCursor.getString(1);
one = mCursor.getString(2);
two = mCursor.getString(3);
three = mCursor.getString(4);
four = mCursor.getString(5);
five = mCursor.getString(6);
six = mCursor.getString(7);
time = mCursor.getString(9);
Bundle bundle = new Bundle();
bundle.putString("mianone", one);
bundle.putString("miantwo", two);
bundle.putString("mianthree", three);
bundle.putString("mianfour", four);
bundle.putString("mianfive", five);
bundle.putString("miansix", six);
bundle.putString("miantime", time);
bundle.putString("modelname", modelname);
Intent intent2 = new Intent();
intent2.putExtras(bundle);
intent2.setClass(ModelActivity.this,
Change.class);
startActivity(intent2);
break;
case 2:
if (c == false) {
Toast.makeText(ModelActivity.this,
getString(R.string.toast_nodelete),
Toast.LENGTH_SHORT).show();
break;
}
if (mCursor.getInt(8) == 2) {
mDateBase.update(getString(R.string.dice_jingdian), 2);
}
Message message = new Message();
message.what = 2;
handler.sendMessage(message);
Toast.makeText(ModelActivity.this,
getString(R.string.toast_delete),
Toast.LENGTH_SHORT).show();
break;
case 3:
break;
default:
break;
}
}
});
dialogBuilder.show();
}
}

class Listener implements OnClickListener {

public void onClick(View v) {
if (v == ImageButton01) {
Intent intent = new Intent();
intent.setClass(ModelActivity.this, AddActivity.class);
startActivity(intent);
}
}

}

public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private View myView;

public MyAdapter(Context c) {
this.inflater = LayoutInflater.from(c);
}

public int getCount() {
return mCursor.getCount();
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
if (strOpt.equals("480*800") || strOpt.equals("600*1024")) {
myView = inflater.inflate(R.layout.user, null);
}
if (strOpt.equals("320*480")) {
myView = inflater.inflate(R.layout.userone, null);
}
mCursor.moveToPosition(position);
final TextView textView = (TextView) myView
.findViewById(R.id.TextView_model);

final TextView textView01 = (TextView) myView
.findViewById(R.id.TextView01);
final CheckBox CheckBox01 = (CheckBox) myView
.findViewById(R.id.CheckBox01);
if (position == 0) {
textView01.setText(R.string.ps_jiawu);
}
if (position == 1) {
textView01.setText(R.string.ps_hejiu);
}
if (position == 2) {
textView01.setText(R.string.ps_zhoumo);
}
if (position == 3) {
textView01.setText(R.string.ps_youxi);
}
if (position == 4) {
textView01.setText(R.string.ps_yundong);
}
if (position == 5) {
textView01.setText(R.string.ps_qinglv);
}
if (position == 6) {
textView01.setText(R.string.ps_duicuo);
}
if (position == 7) {
textView01.setText(R.string.ps_jingdian);
}
if (position > 7) {
textView01.setText(R.string.ps_ziji);
}
int a = mCursor.getInt(8);
textView.setText(mCursor.getString(1));
if (a == 2) {
CheckBox01.setChecked(true);
}
if (a == 1) {
CheckBox01.setChecked(false);
}
CheckBox01.setFocusable(false);
CheckBox01.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
for (int num = 0; num < mCursor.getCount(); num++) {
if (mCursor.moveToPosition(num)
&& mCursor.getInt(8) == 2) {
mDateBase.update(mCursor.getString(1), 1);
}
}
modelname = textView.getText().toString();
mDateBase.update(modelname, 2);
Toast.makeText(ModelActivity.this,
modelname + getString(R.string.toast_defult),
Toast.LENGTH_SHORT).show();
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
});
return myView;
}
}

public Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
listview.invalidateViews();
mCursor.deactivate();
break;
case 2:
mDateBase.delete(modelname);
mCursor = mDateBase.select();
listview.invalidateViews();
break;
}
super.handleMessage(msg);
}
};

public void onDestroy() {
super.onDestroy();
if (mDateBase != null) {
mDateBase.close();
mDateBase = null;
}
}
}
daijope 2011-03-22
  • 打赏
  • 举报
回复
帮助文档很详细,可以去参考一下,我也没有记住。。。
kkkk11kk 2011-03-22
  • 打赏
  • 举报
回复
个人认为可以通过重写一个类继承BaseAdapter来实现你的功能

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧