我正在用CheckBox来解决问题。它在滚动时改变它的状态。在寻找解决方案的一段时间后,我找到了Android: CursorAdapter, ListView and CheckBox,其中getView优先于bindView for CursorAdapters的优化。所以我想在我的case中应用它。所以这是我的代码:
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. 私有ArrayList myList = new ArrayList(); 的用途是什么?
2. 私有光标cur; 未使用。它在Eclipse中显示错误:未使用MainActivity.MyDataAdapter.cur字段的值。
3.为什么有两个CheckBoxes
最终CheckBox cBox =(CheckBox)inView.findViewById(R.id.textcb); // 您的 // CheckBox
和CheckBox cb =(CheckBox)v.findViewById(R.id.textcb);
名为?
答案 0 :(得分:0)
第二点有效,如果没有使用cur,请删除它。
对于第一点,arraylist会跟踪您的位置以及与之相关的值。它是一个名称值列表:name:position1值(true / false /)取决于检查。您可以在构造函数中将所有名称值对初始化为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声明为final。这是次优解决方案。
private 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);
}
}
}