2013-01-06 91 views
1

我試圖從一個SQLite數據庫填充列表視圖我可以創建數據庫,並添加項目,並顯示它們在一個TextView但由於某些原因不能在一個ListView填充的ListView從SQLite的分貝

是它是sData是錯誤的對象類型?

任何人都可以幫忙嗎?

public void DBTest() { 

     SQLiteDatabase myDB = null; 
     String TableName = "myTable"; 

     /* Create a Database. */ 
     try { 
      myDB = this.openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null); 

      /* Create a Table in the Database. */ 
      myDB.execSQL("CREATE TABLE IF NOT EXISTS " 
        + TableName 
        + " (_id integer primary key autoincrement, name text, script text, su short);"); 

      /* Insert data to a Table*/ 
      myDB.execSQL("INSERT INTO " 
        + TableName 
        + " (name, script, su)" 
        + " VALUES ('hello', 'reboot', 1);"); 

      /*retrieve data from database */ 
      Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null); 

      int Column1 = c.getColumnIndex("name"); 
      int Column2 = c.getColumnIndex("script"); 
      int Column3 = c.getColumnIndex("su"); 

      // Check if our result was valid. 
      c.moveToFirst(); 
      String sData=""; 
      if (c != null) { 
       // Loop through all Results 
       do { 
        String Name = c.getString(Column1); 
        String Script = c.getString(Column2); 
        int su = c.getInt(Column3); 
        sData = sData + Name + " " + Script + " " + su + "\n"; 
       } while (c.moveToNext()); 
      } 

      ListView lv = (ListView) findViewById(R.id.mainListView); 
      lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, sData)); 

     } catch (Exception e) { 
      Log.e("Error", "Error", e); 
     } finally { 
      if (myDB != null) 
       myDB.close(); 
     } 
    } 

回答

4

您將以ListView結束,只有一個項目,值爲sData。您需要創建一個列表,例如:

c.moveToFirst(); 
ArrayList<String> sData = new ArrayList<String>(); 
if (c != null) { 
    do { 
     String Name = c.getString(Column1); 
     String Script = c.getString(Column2); 
     int su = c.getInt(Column3); 
     sData.add(Name + " " + Script + " " + su); 
    } while (c.moveToNext()); 
} 

ListView lv = (ListView) findViewById(R.id.mainListView); 
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, sData)); 

我也建議你改變你的光標循環,從而與此類似:

// cursor left as it came from the database because it starts at the row before the first row 
ArrayList<String> sData = new ArrayList<String>(); 
while (c.moveToNext()) { 
    String Name = c.getString(Column1); 
    String Script = c.getString(Column2); 
    int su = c.getInt(Column3); 
    sData.add(Name + " " + Script + " " + su); 
} 

因爲此刻你是不是檢查moveToFirst返回值,它可能會返回false(意味着沒有行),但是你的do-while循環意味着遊標至少被讀取一次,無論它是否有0行,如果有0行,你的應用程序將崩潰。

+0

謝謝你,謝謝你,謝謝你:) – spences10

1
String sData=""; 

嘗試做sData一個String數組。將其饋入適配器。

1

SDATA是沒有數組嘗試像

ArrayList<String> sData = new ArrayList<String>(); 
      if (c != null) { 
       // Loop through all Results 
       do { 
        String Name = c.getString(Column1); 
        String Script = c.getString(Column2); 
        int su = c.getInt(Column3); 
        String newData = Name + " " + Script + " " + su; 
        sData.add(newData); 
       } while (c.moveToNext()); 
      }