它工作正常,我不知道我做了什么改变,我做错了什么,它不再工作了
一切正常,正在创建表,正在插入值,甚至在日志中显示表数据,调用将转到自定义光标适配器,但我通过调试了解到的是
我的自定义光标适配器的 newView 和 Bind 视图方法未调用
在调试期间,我在自定义 cursorAdapter 构造函数中看到值存在于光标中,但 newView 和 bindView 没有调用这里是 mmy 代码:
public class DbHelper extends SQLiteOpenHelper{ private static final String dbName = "(AnnoyingAlarmDatabase).db"; private static final int version = 1; public static final String keyId = "_id"; public static final String hour = "hour"; public static final String minute = "min"; public static final String strength = "strength"; public static final String vibrate = "vibrate"; public static final String snooze = "snooze"; public static final String method = "method"; public static final String label = "label"; public static final String days = "days"; private static final String createTable = "create table alarms (" + keyId + " integer primary key, " + label + " text, " + hour + " integer, " + minute + " integer, " + strength + " text, " + vibrate + " text, " + snooze + " integer, " + method + " text " +")"; public DbHelper(Context context) { // TODO Auto-generated constructor stub super(context, dbName, null, version); Log.d("mydb", "constructor"); } @Override public void onCreate(SQLiteDatabase db) { try{ Log.d("mydb", "oNCreate()"); db.execSQL(createTable); } catch( SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS alarms" ); onCreate(db); } public void addAlarm( String getLabel, int getHour, int getMin, String getStrength, String getVibrate, int getSnooze, String getMethod, String getDays ) { Log.d("mydb", "addAlarm"); SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put( label , getLabel); values.put( hour , getHour); values.put( minute , getMin); values.put( strength , getStrength); values.put( vibrate , getVibrate); values.put( snooze , getSnooze); values.put( method , getMethod); db.insert("alarms", null, values); db.close(); } public Cursor getAlarm( ) { Log.d("mydb", "getAlarm"); SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from alarms", null); try{ while( cursor.moveToNext()) { Log.d("db id", cursor.getString(0)); Log.d("db label",cursor.getString(1)); Log.d("db hour",cursor.getString(2)); Log.d("db minute",cursor.getString(3)); Log.d("db strength",cursor.getString(4)); Log.d("db vibrate",cursor.getString(5)); Log.d("db snooze",cursor.getString(6)); Log.d("db method",cursor.getString(7)); //Log.d("db days",cursor.getString(8)); } } catch( SQLException e ) { e.printStackTrace(); } finally{ // cursor.close(); db.close(); } cursor.moveToFirst(); //db.close(); return cursor; }
活动:
public class ViewAlarms extends ListActivity {
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DbHelper db = new DbHelper(this);
cursor = db.getAlarm();
String [] from = {DbHelper.label};
int [] to = {R.id.textViewLabel};
// SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_view, cursor, from, to);
// setListAdapter(adapter);
CustomCursorAdapter adapter = new CustomCursorAdapter(this, cursor);
setListAdapter(adapter);
cursor.close();
}
自定义光标适配器
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater inflator;
private int hourIndex;
private int minIndex;
private int labelIndex;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
inflator = LayoutInflater.from(context);
hourIndex = c.getColumnIndex(DbHelper.hour);
minIndex = c.getColumnIndex(DbHelper.minute);
labelIndex = c.getColumnIndex(DbHelper.label);
Log.d("customAdapter", "constructor");
//Log.d("adapter curconst",c.getString(minIndex));
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView label = (TextView)view.findViewById(R.id.textViewLabel);
TextView time = (TextView)view.findViewById(R.id.textViewTIme);
Log.d("adapter cursor","chala?");
//time
int hour = Integer.parseInt(cursor.getString(hourIndex));
int min = Integer.parseInt(cursor.getString(minIndex));
String minStr;
if ( min < 10 )
{
minStr = "0" + min;
}
else
{
minStr = "" + min;
}
if ( hour < 12 )
{
if ( hour == 0 )
hour = 12;
time.setText(hour + ":" + minStr + " Am");
}
if ( hour > 12 )
{
if ( hour == 0 )
hour = 12;
time.setText(hour % 12 + ":" + minStr + " Pm");
}
//label
label.setText(cursor.getString(labelIndex));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup arg2) {
Log.d("newView","chala");
return inflator.inflate(R.layout.row_view, arg2, false);
}
}
即使我尝试了简单的光标适配器,它仍然没有显示任何内容。