【发布时间】:2015-06-02 10:26:44
【问题描述】:
我遇到了 CheckBox 的问题。它在滚动时改变了它的状态。在寻找了一段时间的解决方案后,我找到了Android: CursorAdapter, ListView and CheckBox,其中 getView 比 bindView 更适合 CursorAdapters 的优化。所以我想在我的案例中应用它。所以这是我的代码:
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;
}}
}
问题:
1. private ArrayList myList = new ArrayList(); 有什么用?
2. private Cursor cur; 未使用。它在 Eclipse 中显示错误:未使用 MainActivity.MyDataAdapter.cur 字段的值。
3. 为什么会有两个 CheckBox
final CheckBox cBox = (CheckBox) inView.findViewById(R.id.textcb); // 您的 // CheckBox
和CheckBox cb = (CheckBox) v.findViewById(R.id.textcb);
调用 ?我不知道如何使这段代码工作(我是 Android 新手)。
【问题讨论】:
-
用
onCheckChangedListener代替OnClickListener会更好 -
我做到了,但它警告 CompoundButton 类型中的方法 setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) 不适用于参数(新 onCheckChangedListener(){})跨度>
标签: android