Android ListView复选框更改其状态(光标或适配器)
我遇到了一个复选框问题。它会在滚动时更改其状态。在寻找了一段时间的解决方案后,我发现对于CursorAdapters的优化,getView比bindView更受欢迎。因此我考虑将其应用到我的案例中。下面是我的代码:Android ListView复选框更改其状态(光标或适配器),android,Android,我遇到了一个复选框问题。它会在滚动时更改其状态。在寻找了一段时间的解决方案后,我发现对于CursorAdapters的优化,getView比bindView更受欢迎。因此我考虑将其应用到我的案例中。下面是我的代码: package com.example.ki; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.database.Curso
package com.example.ki;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
DBAdapter myDb;
ListView myList;
Cursor c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
display();
insert();
}
private void insert() {
myDb.insertRow("0",false);
myDb.insertRow("1",true);
myDb.insertRow("2",true);
myDb.insertRow("3",false);
myDb.insertRow("4",false);
myDb.insertRow("5",false);
myDb.insertRow("6",false);
myDb.insertRow("7",true);
myDb.insertRow("8",false);
myDb.insertRow("9",false);
myDb.insertRow("10",false);
myDb.insertRow("11",false);
myDb.insertRow("12",false);
myDb.insertRow("13",false);
myDb.insertRow("14",false);
myDb.insertRow("15",false);
myDb.insertRow("16",false);
myDb.insertRow("17",false);
myDb.insertRow("18",false);
myDb.insertRow("19",false);
myDb.insertRow("20",false);
myDb.insertRow("21",false);
myDb.insertRow("22",true);
Toast.makeText(this, "insett(); method called...", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this, null, false);
myDb.open();
}
private void closeDB() {
myDb.close();
}
// Display ListView with CheckBoxes
private void display() {
c = myDb.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_NAME};
int[] toViewIDs = new int[]{R.id.textsv};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter= new SimpleCursorAdapter(getBaseContext(),R.layout.item_layoutt, c , fromFieldNames , toViewIDs ,0);
final ListView myList = (ListView) findViewById(R.id.listView1);
myList.setAdapter(myCursorAdapter);
}
public class MyDataAdapter extends SimpleCursorAdapter {
private Context context;
private ArrayList<String> myList = new ArrayList<String>();
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private Cursor cur;
// itemChecked will store the position of the checked items.
public MyDataAdapter(Context context, int layout, Cursor c, String[] from,
int[] to,int k) {
super(context, layout, c, from, to, k);
this.cur = c;
this.context = context;
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
}
@Override
public View getView(final int pos, View inView, ViewGroup parent) {
if (inView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inView = inflater.inflate(R.layout.item_layoutt, null);
}
final CheckBox cBox = (CheckBox) inView.findViewById(R.id.textcb); // your
// CheckBox
cBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.textcb);
if (cb.isChecked()) {
itemChecked.set(pos, true);
// do some operations here
} else if (!cb.isChecked()) {
itemChecked.set(pos, false);
// do some operations here
}
}
});
cBox.setChecked(itemChecked.get(pos)); // this will Check or Uncheck the
// CheckBox in ListView
// according to their original
// position and CheckBox never
// loss his State when you
// Scroll the List Items.
return inView;
}}
}
package com.example.ki;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Context;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.support.v4.widget.SimpleCursorAdapter;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.CheckBox;
导入android.widget.ListView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
dbadaptermydb;
列表视图myList;
光标c;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
显示();
插入();
}
专用空白插入(){
myDb.insertRow(“0”,false);
myDb.insertRow(“1”,真);
myDb.insertRow(“2”,真);
myDb.insertRow(“3”,假);
myDb.insertRow(“4”,假);
myDb.insertRow(“5”,假);
myDb.insertRow(“6”,假);
myDb.insertRow(“7”,真);
myDb.insertRow(“8”,假);
myDb.insertRow(“9”,假);
myDb.insertRow(“10”,假);
myDb.insertRow(“11”,假);
myDb.insertRow(“12”,假);
myDb.insertRow(“13”,假);
myDb.insertRow(“14”,假);
myDb.insertRow(“15”,假);
myDb.insertRow(“16”,假);
myDb.insertRow(“17”,假);
myDb.insertRow(“18”,假);
myDb.insertRow(“19”,假);
myDb.insertRow(“20”,假);
myDb.insertRow(“21”,假);
myDb.insertRow(“22”,真);
Toast.makeText(这个“insett();方法名为…”,Toast.LENGTH_LONG.show();
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
closeDB();
}
私有void openDB(){
myDb=新的DBAdapter(this,null,false);
myDb.open();
}
私有void closeDB(){
myDb.close();
}
//显示带有复选框的ListView
专用void display(){
c=myDb.getAllRows();
String[]fromFieldNames=新字符串[]{DBAdapter.KEY_NAME};
int[]toViewIDs=newint[]{R.id.textsv};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter=新的SimpleCursorAdapter(getBaseContext(),R.layout.item_layoutt,c,fromFieldNames,toViewId,0);
最终ListView myList=(ListView)findViewById(R.id.listView1);
设置适配器(myCursorAdapter);
}
公共类MyDataAdapter扩展了SimpleCursorAdapter{
私人语境;
private ArrayList myList=new ArrayList();
private ArrayList itemChecked=新建ArrayList();
私有游标cur;
//itemChecked将存储选中项目的位置。
公共MyDataAdapter(上下文上下文、int布局、光标c、字符串[]from、,
int[]至,int k){
super(上下文、布局、c、from、to、k);
this.cur=c;
this.context=上下文;
for(int i=0;i
问题:1.private ArrayList myList=new ArrayList()有什么用强>?
2. 私有游标cur未使用。它显示Eclipse中的错误:未使用MainActivity.MyDataAdapter.cur字段的值。
3.为什么在view.findViewById(R.id.textcb);//你的 //复选框和复选框cb=(复选框)v.findViewById(R.id.textcb)呼叫?
我不知道如何使这段代码工作(我是Android新手)。第二点是有效的,如果不使用cur,请删除它 对于第一点,arraylist会跟踪您的位置以及与之关联的值。这是一个名称值列表:名称:位置1值(真/假/),取决于检查。您可以在构造函数中将所有的名称-值对初始化为false,就像一个新列表一样-无需任何检查 对于第三点,您的
cb
和cBox
都参考相同的视图
cBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.textcb);
if (cb.isChecked()) {
itemChecked.set(pos, true);
// do some operations here
} else if (!cb.isChecked()) {
itemChecked.set(pos, false);
// do some operations here
}
}
});
请注意您的视图是如何传递的:
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.textcb);
看看这些论点。如果你不想做两次,你将不得不宣布cBox为最终版本。这是一个次优的解决方案
以下是一个工作实现:
私有类CurAdapter扩展CursorAdapter实现SectionIndexer{
mAlphabetIndexer字母索引器;
公共CuraAdapter(上下文、游标c、int标志){
super(上下文、c、标志);
对于(int i=0;iprivate class CurAdapter extends CursorAdapter implements SectionIndexer{
AlphabetIndexer mAlphabetIndexer;
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
for (int i = 0; i < c.getCount(); i++) {
itemChecked.add(i, false);
}
mAlphabetIndexer = new AlphabetIndexer(c ,c.getColumnIndex("NotificationDateFor")," ABCDEFGHIJKLMNOPQRTSUVWXYZ");
mAlphabetIndexer.setCursor(c);
}
@Override
public int getPositionForSection(int sectionIndex){
return mAlphabetIndexer.getPositionForSection(sectionIndex);
}
@Override
public int getSectionForPosition(int position){
return mAlphabetIndexer.getSectionForPosition(position);
}
@Override
public Object[] getSections(){
return mAlphabetIndexer.getSections();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
final int position = cursor.getPosition();
String name = (cursor.getString(cursor.getColumnIndexOrThrow("NotificationDateFor")));
String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
String radio = cursor.getString(cursor.getColumnIndexOrThrow("RadioType"));
String type = cursor.getString(cursor.getColumnIndexOrThrow("TypeNotification"));
String friendsname = cursor.getString(cursor.getColumnIndexOrThrow("FriendsName"));
holder.nametext.setTextColor(Color.parseColor("#000000"));
holder.nametext.setText(name);
holder.subtext.setText("");
holder.subtext.setTextColor(Color.parseColor("#000000"));
setImage(image, holder.iv);
holder.chk.setButtonDrawable(id);
holder.chk.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (holder.chk.isChecked()) {
itemChecked.set(position, true);
addlist.setItemChecked(position,true);
} else if (!holder.chk.isChecked()) {
itemChecked.set(position, false);
addlist.setItemChecked(position,false);
}
}
});
holder.chk.setChecked(itemChecked.get(cursor.getPosition()));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.group_list, null);
ViewHolder holder = new ViewHolder(view);
view.setTag(holder);
return view;
}
public class ViewHolder {
TextView nametext, subtext;
RoundedImageView iv;
CheckBox chk;
public ViewHolder(View view){
iv = (RoundedImageView)view.findViewById(R.id.imageView2);
nametext = (TextView) view.findViewById(R.id.textView1);
subtext = (TextView) view.findViewById(R.id.textView2);
chk = (CheckBox) view.findViewById(R.id.checkBox1);
}
}
}