2013-03-12 74 views
0

在我的android項目中,我成功創建了SQLite數據庫,插入操作也成功。 我無法將列表檢索到方法中。無法從SQLIte查詢方法中檢索列表<String>

這裏是我的調用方法:

protected void getTheServerList() { 

     ServerManger info = new ServerManger(this); 

     try { 
      info.open(); 
      servers = info.getData(); 
      info.close(); 

     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "EXCEPTION !", 
        Toast.LENGTH_SHORT).show(); 
     } finally { 

     } 
    } 

這裏是我的SQLite方法

public List<String > getData() { 

    String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_UNAME, 
      KEY_PASSWORD }; 
    List<String> sItems=null; 
    Cursor c = ourDatabase.query(TABLE_NAME, columns, null, null, null, 
      null, null); 
String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iUname = c.getColumnIndex(KEY_UNAME); 
    int iPass = c.getColumnIndex(KEY_PASSWORD); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
    //sItems.add(KEY_NAME); 
     result =c.getString(iName); 

     sItems.add(result); 


    } 

    c.close(); 




    return sItems; 
} 

,我得到一個Exception

03-12 12:19:15.798: E/Database(390): close() was never explicitly called on database '/data/data/com.example.proj/databases/PROJ_DB' 

我可以檢索的字符串,但不清單

請幫忙!

+0

你得到空指針異常? – sandy 2013-03-12 07:21:02

回答

0

啓動列表..名單null

List<String> sItems=new ArrayList<String>(); 
+0

'new List ();'應該可能是ArrayList或LinkedList like'new ArrayList ();' – Vinay 2013-03-12 07:31:19

+0

現在看起來不錯。 – Vinay 2013-03-12 07:36:23

+0

@BaZinga謝謝 – user2159749 2013-03-12 07:56:18

0
public List<String > getData() { 
    String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_UNAME, 
      KEY_PASSWORD }; 
    List<String> sItems=new ArrayList<String> // define instance Here you create listview but not creat this instance 
    Cursor c = ourDatabase.query(TABLE_NAME, columns, null, null, null, 
      null, null); 
String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iUname = c.getColumnIndex(KEY_UNAME); 
    int iPass = c.getColumnIndex(KEY_PASSWORD); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
    //sItems.add(KEY_NAME); 
     result =c.getString(iName); 

     sItems.add(result); 
    } 
    c.close(); 
    return sItems; 
} 
+1

你每次都嘗試實例化列表。在一個循環中。所以列表將只有一個元素。它應該在循環之前我猜 – Vinay 2013-03-12 07:32:41

+0

@JJPA是的..我編輯了這個。 – QuokMoon 2013-03-12 07:33:42