我有一个包含每一行上有一个复选框一个TextView一个列表视图,所以当复选框被选中,我们通过列表视图的复选框实例将采取从一个地方到另一个地方向下滚动(重复使用..)和我有几个检查复选框如何解决,我想绑定的复选框的列表视图,但没有工作,我的code是:
SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.rating,铜,新的String [] {标题,收藏夹},新的INT [] {R.id .text1,R.id.bt_rating},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listv.setAdapter(适配器);
adapter.setViewBinder(新SimpleCursorAdapter.ViewBinder(){
/ **通过绑定指定索引到指定的视图*定义光标列/
公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
如果(view.getId()== R.id.bt_rating){
((复选框)视图).setChecked(通过Boolean.valueOf(cursor.getString(cursor.getColumnIndex(最爱))));
((复选框)视图).setOnCheckedChangeListener(myCheckChangList);
返回true; //正确的,因为数据被绑定到视图
}
返回false;
}
});
OnCheckedChangeListener myCheckChangList =新OnCheckedChangeListener(){
公共无效onCheckedChanged(CompoundButton buttonView,
布尔器isChecked){
buttonView.setChecked(器isChecked);
}
};
我的列表视图的列的内容我的XML code是:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
<复选框
机器人:ID =@ + ID / bt_rating
机器人:可聚焦=假
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =center_vertical
机器人:按钮=@机器人:可绘制/ btn_star/>
<的TextView
机器人:ID =@ + ID / text1中
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =@扪/ fsinlistview
/>
< / LinearLayout中>
解决方案
它看起来像你的 OnCheckedChangedListener
是这里的问题。如果你看一下你的code,看到的每次的复选框得到一个参考的一样的监听器。所以,当你选中一个框,你设置隔框,选中太 - 和你不更新数据的支持,无论是
您 OnCheckedChangedListener
不应该更新复选框的视图状态 - 回调被激发,因为国家已经改变了
所以,你需要当用户检查复选框,请执行下列操作步骤:
在找出被检查哪些项目,以及如何对应于您的数据 更新您的数据,以适应新的选中/取消选中状态 通知您的数据更改的适配器/更新你的光标您可以做到这一点类似于下面的东西,标记的看法与它重新presents行的ID:
公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
如果(view.getId()== R.id.bt_rating){
view.setTag(cursor.getInt(cursor.getColumnIndex(SomeDBContract.ID)));
((复选框)视图).setChecked(通过Boolean.valueOf(cursor.getString(cursor.getColumnIndex(最爱))));
((复选框)视图).setOnCheckedChangeListener(myCheckChangList);
返回true; //正确的,因为数据被绑定到视图
}
返回false;
}
然后,在你的听众可以根据该ID更新数据库:
CheckedChangeListener myCheckChangList =新OnCheckedChangeListener(){
公共无效onCheckedChanged(CompoundButton buttonView,
布尔器isChecked){
INT ROWID =(INT)buttonView.getTag();
//处理更新数据库按正常
updateSomeDbRowAsChecked(ROWID,器isChecked);
}
};
最后,你需要有一个新的光标更新你的光标适配器一旦数据库更新行:
myAdapter.swapCursor(newCursor);
您将不得不调整这一切,以满足您的code,但它应该给你的,你可以解决这个问题的一种方式的想法。
I have a listview containing on each row a textview with a checkbox, so when the checkbox is checked and we scroll down through the listview the checkbox instance will be taken from a place to another (reused..) and I have several checked checkboxes how to fix that I tried to bind the checkbox to the listview but that didn't work my code is:
SimpleCursorAdapter adapter =new SimpleCursorAdapter(this,R.layout.rating,cu,new String[]{"Title","Favorites"}, new int[]{R.id.text1,R.id.bt_rating},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listv.setAdapter(adapter);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
/** Binds the Cursor column defined by the specified index to the specified view */
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(view.getId() == R.id.bt_rating){
((CheckBox)view).setChecked(Boolean.valueOf(cursor.getString(cursor.getColumnIndex("Favorites"))));
((CheckBox)view).setOnCheckedChangeListener(myCheckChangList);
return true; //true because the data was bound to the view
}
return false;
}
});
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
buttonView.setChecked(isChecked);
}
};
My xml code of the content of the row of my listview is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/bt_rating"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:button="@android:drawable/btn_star"/>
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/fsinlistview"
/>
</LinearLayout>
解决方案
It looks like your OnCheckedChangedListener
is the problem here. If you look at your code, see that every checkbox is getting a reference to the same listener. So when you check one box, you're setting every other box as checked too - and you're not updating your backing data, either.
Your OnCheckedChangedListener
should not be updating the view state of the checkbox - the callback is fired because the state has already changed.
So you need to do the following steps when a user checks the checkbox:
Figure out which item was checked, and how that corresponds to your data Update your data to suit the new checked/unchecked state Notify your adapter of a data change/update your cursorYou could do this something like the following, tagging the view with the ID of the row it represents:
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(view.getId() == R.id.bt_rating){
view.setTag(cursor.getInt(cursor.getColumnIndex(SomeDBContract.ID)));
((CheckBox)view).setChecked(Boolean.valueOf(cursor.getString(cursor.getColumnIndex("Favorites"))));
((CheckBox)view).setOnCheckedChangeListener(myCheckChangList);
return true; //true because the data was bound to the view
}
return false;
}
Then, in your listener you can update your database according to that ID:
CheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int rowId = (int) buttonView.getTag();
// Handle updating the database as per normal
updateSomeDbRowAsChecked(rowId, isChecked);
}
};
Finally, you'll need to update your cursor adapter with a new cursor once the database row is updated:
myAdapter.swapCursor(newCursor);
You'll have to adjust all of this to suit your code, but it should give you an idea of one way you can approach this problem.