I want to populate a list from my database.
I have the following code:
setContentView(R.layout.pro);
addProFromDB();
}
How I should populate the multi 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()));
It is not giving any error, but not showing the inserted items either.
Number of issues are there in your code snippet.
tbl_productevery time you run you app.ArrayAdapterasnew ArrayList()I would recommend you to go through any tutorial, and sort out things step by step.
I’m suggesting you to follow this tutorial, and use
CursorAdapterwhen you are working with sqlite.