我已经看到了其他类似的问题,但没有任何解决问题。 我的问题是Checkboxes和CheckedTextViews从我的列表中随机消失。也就是说,它们是第一次加载ListView,以及第一次滚动到底部。任何随机滚动都会导致CheckBoxes退出并消失。
以下是我的扩展SimpleCursorAdapter的代码。
public class CheckBoxCursorAdapter extends SimpleCursorAdapter{
static final String TAG = "CheckBoxCursorAdapter";
final Context contextMain;
Cursor c;
SQLiteDatabase db;
public CheckBoxCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, SQLiteDatabase db) {
super(context, layout, c, from, to);
this.contextMain = context;
this.c = c;
this.db = db;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
CheckedTextView cb = (CheckedTextView)view.findViewById(R.id.cb);
cb.setOnClickListener(null);
TextView cbFull = (TextView)view.findViewById(R.id.fullname);
TextView cbAbbrev = (TextView)view.findViewById(R.id.abbrev);
cb.setCheckMarkDrawable(android.R.drawable.btn_default);
final String abbrv = cursor.getString(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_ABBRV));
cbFull.setText(cursor.getString(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_NAME)));
cbAbbrev.setText(abbrv);
cb.setChecked(cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_DISPLAYED)) == 1 ? true : false);
Log.v(TAG, "displayed = " + Long.toString(cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_DISPLAYED))));
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckedTextView) v).isChecked()) {
Log.v(TAG, "Calling isChecked==true!");
db.execSQL("UPDATE buildinglist SET displayed='1' WHERE abbrv='"+ abbrv +"'");
((CheckedTextView) v).setChecked(true);
}
else {
Log.v(TAG, "Calling isChecked==false!");
db.execSQL("UPDATE buildinglist SET displayed='0' WHERE abbrv='"+ abbrv +"'");
//(CheckedTextView) v).setChecked(false);
((CheckedTextView) v).setChecked(false);
}
}
});
final int latitude = cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_YCOORD));
final int longitude = cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_XCOORD));
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Log.v(TAG, "Calling isChecked==true!");
// db.execSQL("UPDATE buildinglist SET displayed='1' WHERE abbrv='"+ abbrv +"'");
// ((CheckedTextView) v).setChecked(true);
Intent i = new Intent(contextMain,View_map.class);
i.putExtra("lat", latitude);
i.putExtra("long", longitude);
Log.d(TAG,"Latitude onLongClick" + latitude + "");
Log.d(TAG,"Longitude onLongClick" + longitude +"");
contextMain.startActivity(i);
return false;
}
});
}
}
我正在使用ListActivity,这是启动。
public class Buildings_List extends ListActivity{
BuildingOpenHelper opener;
SQLiteDatabase db;
SQLiteDatabase dbWrite;
static final String[] FROM = {BuildingOpenHelper.C_NAME, BuildingOpenHelper.C_ABBRV, BuildingOpenHelper.C_DISPLAYED};
static final int[] TO = { R.id.fullname, R.id.abbrev, R.id.cb };
private static final String TAG = "Buildings_List";
SimpleCursorAdapter adapt;
Cursor myCur = null;
Bundle savedInstanceState2;
@Override
public void onCreate(Bundle savedInstanceState) {
savedInstanceState2 = savedInstanceState;
super.onCreate(savedInstanceState2);
ListView awesome = getListView();
opener = new BuildingOpenHelper(this);
db = opener.getReadableDatabase();
dbWrite = opener.getWritableDatabase();
try {
myCur = db.query(BuildingOpenHelper.TABLE,null,null,null,null,null,BuildingOpenHelper._ID + " DESC");
}
catch(SQLException e){
Log.d(TAG,"Query Went Bad");
}
startManagingCursor(myCur);
//using depracated SimpleCursorAdapter. Not quite sure what the flags need to be when using updated constructor
adapt = new CheckBoxCursorAdapter(this, R.layout.buildinglisting, myCur, FROM, TO, dbWrite);
setListAdapter(adapt);
}
我知道onClickListeners不是问题,因为我已经尝试删除它们,问题仍然存在。我已经阅读了CheckBoxes的多个帐户没有正确回收,但问题仍然存在于CheckTextViews。我错过了一些非常简单的事情吗?