在Android的列表视图复选框麻烦 [英] listview checkbox trouble in android
问题描述
我有ListView的麻烦。我添加了一个复选框到列表视图选择项。我的数据是从源码来了,所以我用简单的游标适配器。我的名单长度aproximatley 250线。我点击一张支票box.when我在屏幕上显示向下滚动,复选框,单击每10行。(页面(列表)例如10行数据时,我滚动11行,该行的复选框,点击了,我该怎么解决此问题
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<复选框
机器人:ID =@ + ID /复选框
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
/>
<的TextView
机器人:ID =@ + ID /名称
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ + ID /复选框
机器人:TEXTSTYLE =黑体
机器人:文本=TextView的/>
<的TextView
机器人:ID =@ + ID /尼克
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID /尼克
机器人:layout_alignBottom =@ + ID /尼克
机器人:layout_alignParentRight =真
机器人:文本=TextView的/>
<的TextView
机器人:ID =@ + ID /电话
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /名称
机器人:layout_toRightOf =@ + ID /复选框
机器人:文本=TextView的/>
< / RelativeLayout的>
这是源$ C $ C 包com.example.myprojects;
进口android.app.Activity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.support.v4.widget.SimpleCursorAdapter;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.widget.EditText;
进口android.widget.ListView;
公共类send_message延伸活动{
私人的ListView列表;
私人的EditText搜索;
SimpleCursorAdapter adapterx;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_sendmessage);
名单=(ListView控件)findViewById(R.id.list);
搜索=(EditText上)findViewById(R.id.search);
loadfromdatabase();
}
私人无效loadfromdatabase(){
MYDB信息=新MYDB(本);
info.open_read();
光标C = info.getAllData();
的String []列=新的String [] {mydb.KEY_NAME,mydb.KEY_PHONE,mydb.KEY_NICK};
INT []到=新INT [] {R.id.name,R.id.phone,R.id.nick};
@燮pressWarnings(德precation)
SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.activity_sendmesage_rows,C柱,至,0);
list.setAdapter(适配器);
info.close();
}
}
如果你有复选框
ES在的ListView
你有问题,因为的ListView
重用查看
s表示尚未显示在屏幕上。这就是为什么当你向下滚动,它加载(几次),一个复选框
,是由您检查previously。所以,你不能依赖于默认复选框
的行为。你需要的是:
-
从检查prevent用户
复选框
。您可以通过调用做到这一点cb.setEnabled(假)
,其中CB
是复选框
您正在使用。 -
创建自己的适配器类,将延长
SimpleCursorAdapter
。在这个类中,你将存储列表
的项目进行检查的指标。 -
覆盖
getView()
方法,在你的适配器类,并有手动设置复选框
如果要进行检查它的位置存储在检查的项目数组中。 -
创建
OnClickListener
您的ListView
这会照顾的添加/删除选中的项目立场适配器的内部数组。
编辑:
这将是你的适配器的code:
公共类MySimpleCursorAdapter扩展SimpleCursorAdapter {
公众的ArrayList<整数GT; mItemsChecked;
公共MySimpleCursorAdapter(上下文的背景下,INT布局,光标C,
的String []从,INT []键){
超(背景下,布局,C,从,到);
mItemsChecked =新的ArrayList<整数GT;();
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
的for(int i = 0; I< mItemsChecked.size();我++){
如果(mItemsChecked.get(ⅰ)==位置){
复选框CB =(复选框)convertView.findViewById(R.id.checkBox1); //相反checkBox1的,写CheckBox的你的名字
cb.setChecked(真正的);
}
}
返回super.getView(位置,convertView,父母);
}
/ *只是一个方便的方法,这将是负责添加/删除
项目从mItemsChecked列表中的位置* /
公共无效itemClicked(INT位置){
的for(int i = 0; I< mItemsChecked.size();我++){
如果(mItemsChecked.get(ⅰ)==位置){
mItemsChecked.remove(ⅰ);
返回;
}
}
mItemsChecked.add(位置);
}
}
这是 OnItemClickListener
您的ListView
:
最终的ListView LV =(ListView控件)findViewById(R.id.listView1); //你的ListView的名字
lv.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3){
((MySimpleCursorAdapter)lv.getAdapter())itemClicked(位置)。
lv.getAdapter()notifyDataSetChanged()。 //因为我们需要调用getView()方法中的ListView所有可见的物品,以使它们被更新
}
});
不过,别忘了禁用复选框
,以便用户不能点击它。
现在,你可以把它看成一个小黑客,因为用户不点击复选框
在所有 - 应用程序认为它是点击特定的的ListView
的项目,从而自动检查相应的复选框
给你。
i have a trouble with listview. i added a checkbox into listview to choose items. my data is coming from sqlite so i use simple cursor adapter. length of my list is aproximatley 250 lines. i am clicking a check box.when i scroll down page (list), checkbox is clicked in every 10 lines.(for example in my screen show 10 lines data when i scroll 11th lines, this row's checkbox had clicked. how can i solve this problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBox"
android:textStyle="bold"
android:text="TextView" />
<TextView
android:id="@+id/nick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nick"
android:layout_alignBottom="@+id/nick"
android:layout_alignParentRight="true"
android:text="TextView" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_toRightOf="@+id/checkBox"
android:text="TextView" />
</RelativeLayout>
and this is source code package com.example.myprojects;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
public class send_message extends Activity {
private ListView list;
private EditText search;
SimpleCursorAdapter adapterx;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sendmessage);
list=(ListView)findViewById(R.id.list);
search=(EditText)findViewById(R.id.search);
loadfromdatabase();
}
private void loadfromdatabase() {
mydb info=new mydb(this);
info.open_read();
Cursor c = info.getAllData();
String[] columns = new String[] {mydb.KEY_NAME,mydb.KEY_PHONE, mydb.KEY_NICK};
int[] to = new int[] {R.id.name,R.id.phone, R.id.nick };
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter= new SimpleCursorAdapter(this, R.layout.activity_sendmesage_rows, c, columns, to,0);
list.setAdapter(adapter);
info.close();
}
}
If you have CheckBox
es in ListView
you have problem, because ListView
reuses View
s that aren't already visible on screen. That's why when you scroll down, it loads (a few times) that one CheckBox
that was checked by you previously. So you can't rely on default CheckBox
's behaviour. What you need is:
Prevent user from checking
CheckBox
. You can do this by callingcb.setEnabled(false)
, wherecb
isCheckBox
which you are using.Create your own adapter class that will extend
SimpleCursorAdapter
. In this class you will storelist
of indexes of items that are checked.Override
getView()
method in your adapter class and there manually setCheckBox
to be checked if it's position is stored in checked items array.Create
OnClickListener
for yourListView
which will take care of adding/removing checked items' positions from adapter's internal array.
Edit:
This would be the code of your adapter:
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
public ArrayList<Integer> mItemsChecked;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
mItemsChecked = new ArrayList<Integer>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
for(int i = 0; i < mItemsChecked.size(); i++) {
if(mItemsChecked.get(i) == position) {
CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1); // Instead of checkBox1, write your name of CheckBox
cb.setChecked(true);
}
}
return super.getView(position, convertView, parent);
}
/* Just a handy method that will be responsible for adding/removing
items' positions from mItemsChecked list */
public void itemClicked(int position) {
for(int i = 0; i < mItemsChecked.size(); i++) {
if(mItemsChecked.get(i) == position) {
mItemsChecked.remove(i);
return;
}
}
mItemsChecked.add(position);
}
}
This is OnItemClickListener
for your ListView
:
final ListView lv = (ListView)findViewById(R.id.listView1); // Your ListView name
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
((MySimpleCursorAdapter)lv.getAdapter()).itemClicked(position);
lv.getAdapter().notifyDataSetChanged(); // Because we need to call getView() method for all visible items in ListView, so that they are updated
}
});
Just remember to disable your CheckBox
so that user can't click it.
Now you can see it as a little hack, because user doesn't click CheckBox
at all - application sees it as clicking in specific ListView
item, which in turn automatically checks appropriate CheckBox
for you.
这篇关于在Android的列表视图复选框麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!