listview 选中一个checkbox为什么会自动选中多个checkbox listview中没行都有一个checkbox,为什么我选中一个checkbox而有的checkbox会自动选中呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哪块没处理正确吧,listview中用checkbox肯定是没问题的。 这是部分代码第一部分代码 List<HashMap<String,Object>> lStudent = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> hm = new HashMap<String,Object>(); HashMap<String,Object> hm1 = new HashMap<String,Object>(); HashMap<String,Object> hm2 = new HashMap<String,Object>(); HashMap<String,Object> hm3= new HashMap<String,Object>(); HashMap<String,Object> hm4 = new HashMap<String,Object>(); HashMap<String,Object> hm5 = new HashMap<String,Object>(); HashMap<String,Object> hm6 = new HashMap<String,Object>(); HashMap<String,Object> hm7 = new HashMap<String,Object>(); HashMap<String,Object> hm8 = new HashMap<String,Object>(); HashMap<String,Object> hm9= new HashMap<String,Object>(); HashMap<String,Object> hm10 = new HashMap<String,Object>(); HashMap<String,Object> hm11 = new HashMap<String,Object>(); hm.put("callid", "1"); hm.put("name", "张三1"); hm1.put("callid", "2"); hm1.put("name", "李四1"); hm2.put("callid", "3"); hm2.put("name", "王五1"); hm3.put("callid", "4"); hm3.put("name", "张三2"); hm4.put("callid", "5"); hm4.put("name", "李四2"); hm5.put("callid", "6"); hm5.put("name", "王五2"); hm6.put("callid", "7"); hm6.put("name", "张三3"); hm7.put("callid", "8"); hm7.put("name", "李四3"); hm8.put("callid", "9"); hm8.put("name", "王五3"); hm9.put("callid", "10"); hm9.put("name", "张三4"); hm10.put("callid", "11"); hm10.put("name", "李四4"); hm11.put("callid", "12"); hm11.put("name", "王五4"); lStudent.add(hm); lStudent.add(hm1); lStudent.add(hm2); lStudent.add(hm3); lStudent.add(hm4); lStudent.add(hm5); lStudent.add(hm6); lStudent.add(hm7); lStudent.add(hm8); lStudent.add(hm9); lStudent.add(hm10); lStudent.add(hm11); return lStudent;----------------------------------------第二部分代码<ListView android:id="@+id/timerlist" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="68dp" android:stretchMode="columnWidth" android:gravity="center" /> -------------------------------------------第三部分代码List<HashMap<String, Object>> lStudent = StudentDao.getList(null, null); ListView lv = (ListView)this.findViewById(R.id.timerlist); SimpleAdapter sa = new SimpleAdapter (this, lStudent, R.layout.timeritem, new String[]{"callid","name"}, new int[]{R.id.dsdate,R.id.dstime}); lv.setAdapter(sa); 第四部分代码<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="140px" android:layout_height="wrap_content" android:id="@+id/dsdate" android:textSize="18dp" android:paddingLeft="6px" android:textColor="@color/whilte" android:layout_marginTop="6px" /> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:id="@+id/dstime" android:textColor="@color/whilte" android:layout_marginTop="6px" android:textSize="18dp" android:paddingLeft="20px" android:layout_toRightOf="@+id/dsdate" /> <CheckBox android:layout_toRightOf="@+id/dstime" android:id="@+id/dsid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:textColor="@color/whilte" android:paddingLeft="10px" android:focusable="false" /></RelativeLayout> 这类帖子我回过几次了。系统会把出屏的View用来充当新入屏的View(这样不用再new了,只修改一下界面文字),这是优化。所以有check框的view,你必须自己实现adapter,在每次getView的时候,根据情况,去打勾或者去掉勾。 http://topic.csdn.net/u/20110316/17/32dc2d09-8088-4374-be16-0ba652afa67f.html看这个吧,我费了好多功夫,也不知道到底解释清楚了没有。 我遇到分页的时候,选中的checkbox直接就消失了 你的adapter内部的部分内容被释放了,adapter应该内部自己实现一个数组的,并且是成员的,不然内存吃紧, 同遇到这个问题!!!主要是因为 getView 的问题。你要继承Adapter ,构造函数中需要 初始化 一个map<Integer,Boolean>,去对应每个位置的checkbox,尤其要重写getView方法!getView 方法中,还需要去改变map的值!让它记住上次checkbox的状态。希望对你有帮助。 http://blog.sina.com.cn/s/blog_6fff321b0100nyjs.html!!!这个链接也有解决方法~~~我的问题已经解决了!哈哈~~~ 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行的也同时会被点选,所以可以 1) getView(int position, View convertView, ViewGroup parent){} 方法每行的开始 写这样一行convertView = null; 2) 用一个List记录你勾选过的每一行的LiseView index android连接facebook上传图片到公共主页的问题 javah 提示 android.app.Activity 找不到 图片过大,如何使bitmap自适应填充我画的矩形? 请教android sdk无法更新的问题 如何控制view的隐藏和显示 camera系统代码中的CE是什么意思? 设置ListView的背景颜色后,整个ListView变成了黑色。 谁给一个DES加密解密算法,带注释的 服务器 socket通信问题 请问可以修改项目的分辨率么? 昨天开发了一个Google Map,居然不显示,望高手指点指点 静态注册BroadcastReceiver没有响应
这是部分代码
第一部分代码
List<HashMap<String,Object>> lStudent = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> hm = new HashMap<String,Object>();
HashMap<String,Object> hm1 = new HashMap<String,Object>();
HashMap<String,Object> hm2 = new HashMap<String,Object>();
HashMap<String,Object> hm3= new HashMap<String,Object>();
HashMap<String,Object> hm4 = new HashMap<String,Object>();
HashMap<String,Object> hm5 = new HashMap<String,Object>();
HashMap<String,Object> hm6 = new HashMap<String,Object>();
HashMap<String,Object> hm7 = new HashMap<String,Object>();
HashMap<String,Object> hm8 = new HashMap<String,Object>();
HashMap<String,Object> hm9= new HashMap<String,Object>();
HashMap<String,Object> hm10 = new HashMap<String,Object>();
HashMap<String,Object> hm11 = new HashMap<String,Object>();
hm.put("callid", "1");
hm.put("name", "张三1");
hm1.put("callid", "2");
hm1.put("name", "李四1");
hm2.put("callid", "3");
hm2.put("name", "王五1");
hm3.put("callid", "4");
hm3.put("name", "张三2");
hm4.put("callid", "5");
hm4.put("name", "李四2");
hm5.put("callid", "6");
hm5.put("name", "王五2");
hm6.put("callid", "7");
hm6.put("name", "张三3");
hm7.put("callid", "8");
hm7.put("name", "李四3");
hm8.put("callid", "9");
hm8.put("name", "王五3");
hm9.put("callid", "10");
hm9.put("name", "张三4");
hm10.put("callid", "11");
hm10.put("name", "李四4");
hm11.put("callid", "12");
hm11.put("name", "王五4");
lStudent.add(hm);
lStudent.add(hm1);
lStudent.add(hm2);
lStudent.add(hm3);
lStudent.add(hm4);
lStudent.add(hm5);
lStudent.add(hm6);
lStudent.add(hm7);
lStudent.add(hm8);
lStudent.add(hm9);
lStudent.add(hm10);
lStudent.add(hm11);
return lStudent;
----------------------------------------
第二部分代码<ListView
android:id="@+id/timerlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="68dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
-------------------------------------------第三部分代码List<HashMap<String, Object>> lStudent = StudentDao.getList(null, null);
ListView lv = (ListView)this.findViewById(R.id.timerlist);
SimpleAdapter sa = new SimpleAdapter
(this, lStudent, R.layout.timeritem, new String[]{"callid","name"}, new int[]{R.id.dsdate,R.id.dstime});
lv.setAdapter(sa);
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="140px"
android:layout_height="wrap_content"
android:id="@+id/dsdate"
android:textSize="18dp"
android:paddingLeft="6px"
android:textColor="@color/whilte"
android:layout_marginTop="6px"
/>
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/dstime"
android:textColor="@color/whilte"
android:layout_marginTop="6px"
android:textSize="18dp"
android:paddingLeft="20px"
android:layout_toRightOf="@+id/dsdate"
/>
<CheckBox android:layout_toRightOf="@+id/dstime"
android:id="@+id/dsid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:textColor="@color/whilte"
android:paddingLeft="10px"
android:focusable="false"
/></RelativeLayout>
系统会把出屏的View用来充当新入屏的View(这样不用再new了,只修改一下界面文字),这是优化。所以有check框的view,你必须自己实现adapter,在每次getView的时候,根据情况,去打勾或者去掉勾。
主要是因为 getView 的问题。
你要继承Adapter ,构造函数中需要 初始化 一个map<Integer,Boolean>,去对应每个位置的checkbox,尤其要重写getView方法!
getView 方法中,还需要去改变map的值!让它记住上次checkbox的状态。
希望对你有帮助。
我的问题已经解决了!哈哈~~~
当光标下移到屏幕最底部,新显示出来的View,最初的3个(不知道为什么是3个),在Adapter中调用getView(int position, View convertView, ViewGroup parent)方法中,会判断convertView为null,而再有新的View就会发现convertView不为空,所以新显示的View其实使用了之前某个View的对象。这就造成了状态可能混乱。比如第一行的checkbox点选时,第11行的也同时会被点选,
所以可以
1) getView(int position, View convertView, ViewGroup parent){}
方法每行的开始 写这样一行convertView = null;
2) 用一个List记录你勾选过的每一行的LiseView index