无法从SQLIte查询方法</string>中检索List <string>

时间:2013-03-12 07:14:50

标签: android sqlite

在我的android项目中,我已经成功创建了一个SQLite数据库,插入操作也很成功。 我无法将List检索到方法中。

这是我的调用方法:

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' 

我可以检索字符串,但不能检索列表

请帮忙!

2 个答案:

答案 0 :(得分:0)

发起您的列表..列表为null

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

答案 1 :(得分: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;
}