我想从我的数据库中填充一个列表。
我有以下代码:
setContentView(R.layout.pro);
addProFromDB();
}如何填充多个xCodexInlinexPlacexHolderx
private void addProFromDB() {
// TODO Auto-generated method stub
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase sampleDB = null;
try {
list = (ListView)findViewById(android.R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, 1, null);
sampleDB.execSQL("create table tbl_product("
+ "pro_id integer PRIMARY KEY autoincrement,"
+ "pro_name text," + "pro_price integer);");
sampleDB.execSQL("insert into tbl_product(pro_name, pro_price) values ('oil', '200' );");
Cursor c = sampleDB.query(SAMPLE_TABLE_NAMES, null, null, null, null, null,
null);
char pro_nameColumnIndex = (char) c.getColumnIndexOrThrow("pro_name");
int pro_priceColumnIndex = (int) c.getColumnIndexOrThrow("pro_price");
} finally {
if (sampleDB != null)
sampleDB.close();
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, new ArrayList()));它没有给出任何错误,但也没有显示插入的项。
发布于 2012-05-03 08:08:57
在您的代码片段中存在许多问题。
tbl_product。new ArrayList()传递给您的new ArrayList()。我会建议你通过任何一个教程,并整理事情一步一步。
我建议您遵循本教程,在使用sqlite时使用CursorAdapter。
发布于 2012-05-03 08:28:05
我要修改你的代码,现在你可以检查和使用这段代码了,这可能对你有帮助。
private void addProFromDB() {
// TODO Auto-generated method stub
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase sampleDB = null;
try {
list = (ListView)findViewById(android.R.id.list);
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, 1, null);
sampleDB.execSQL("create table if not exists tbl_product("
+ "pro_id integer PRIMARY KEY autoincrement,"
+ "pro_name text," + "pro_price integer);");
sampleDB.execSQL("insert into tbl_product(pro_name, pro_price) values ('oil', '200' );");
Cursor c = sampleDB.query(SAMPLE_TABLE_NAMES, null, null, null, null, null,
null);
char pro_nameColumnIndex = (char) c.getColumnIndexOrThrow("pro_name");
int pro_priceColumnIndex = (int) c.getColumnIndexOrThrow("pro_price");
ArrayList list = new ArrayList()
list.add(pro_nameColumnIndex);
list.add(pro_priceColumnIndex);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, list));
} finally {
if (sampleDB != null)
sampleDB.close();
}https://stackoverflow.com/questions/10427346
复制相似问题