2012-05-03 51 views
0

我想從我的數據庫填充一個列表。從SQLite填充CheckList

我有以下代碼:

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())); 

它不給予任何錯誤,但沒有表現出任何插入的項目。

回答

1

問題的數量存在於您的代碼片段中。

  1. 您每次運行應用程序時都會創建表tbl_product
  2. 您每次都在新創建的表格中插入新元素。
  3. 終於要傳遞空數組到您ArrayAdapternew ArrayList()

我會建議你去通過任何教程,並理清事情一步一步來。

我建議你關注this tutorial,並在使用sqlite時使用CursorAdapter

+0

感謝您sugestions..i知道使用此代碼的AV不錯的辦法,但在我的情況它滿足requiremnt ..我有3個選項卡,並在每個選項卡上單擊其相關的表格創建..你能幫我我應該用什麼部分來填補這個空陣列...謝謝 – Numair

+0

[從這裏開始快速解決方案](http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial_database) –

0

我要修改代碼,現在你可以檢查和,這可能有助於你..

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(); 
} 
+0

它不顯示任何數據...空白屏幕 – Numair

+0

請通過本教程...清除所有概念.. http ://www.vogella.com/article S/AndroidSQLite/article.html#概述 –