在我的Android應用程序中,我從SQLite數據庫中讀取了一些數據,並嘗試將其顯示到列表視圖中。這裏是我的代碼:ListView與Cusror適配器不顯示任何內容
ListView listContent;
SQLiteAdapterno nadapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ofrnumber);
listContent=(ListView)findViewById(R.id.listView1);
nadapter=new SQLiteAdapterno(this);
nadapter.openToRead();
Cursor c=nadapter.queueAll();
String[] from = new String[]{SQLiteAdapterno.KEY_ID, SQLiteAdapterno.KEY_RCODE, SQLiteAdapterno.KEY_RNAME,SQLiteAdapterno.KEY_OFNO};
int[] to = new int[]{R.id.id,R.id.text1,android.R.id.text2,android.R.id.text2};
SimpleCursorAdapter cursorAdapter =new SimpleCursorAdapter(this, R.layout.row,c, from, to);
listContent.setAdapter(cursorAdapter);
}
SQLiteAdapterno:
public class SQLiteAdapterno {
public static final String MYDATABASE_NAME2 = "MY_DATABASEOFRN";
public static final String MYDATABASE_TABLE2 = "MY_OFFERNO";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_RCODE = "rcode";
public static final String KEY_OFNO = "ofno";
public static final String KEY_RNAME = "rname";
public static final String KEY_ID = "_id";
private static final String SCRIPT_CREATE_DATABASE1 =
"create table " + MYDATABASE_TABLE2 + " ("
+ KEY_ID +" integer primary key autoincrement, "
+ KEY_RCODE + " text, "
+ KEY_RNAME + " text, "
+ KEY_OFNO + " text);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapterno(Context c)
{
context=c;
}
public SQLiteAdapterno openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapterno openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String rcode, String rname, String ofno){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_RCODE, rcode);
contentValues.put(KEY_RNAME, rname);
contentValues.put(KEY_OFNO, ofno);
return sqLiteDatabase.insert(MYDATABASE_TABLE2, null, contentValues);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_RCODE, KEY_RNAME, KEY_OFNO};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE2, columns,
null, null, null, null, null);
return cursor;
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE2, null, null);
}
我沒有發現的結果,它不會顯示在列表視圖的任何項目。有人可以說這個代碼有什麼錯誤,以及如何解決它?
你確定你的光標包含的東西?例如log c.getCount() – 2013-03-18 12:03:18
可以打印c.getCount(),from.size,to.size? – Harshid 2013-03-18 12:05:13
是的,我沒有檢查它。光標爲空 – 2013-03-18 12:14:59