2013-03-12 49 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