0

在我的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' 

我可以检索 String ,但不能检索 - List

请帮忙 !

4

2 回答 2

0

启动您的列表..列表是null

List<String> sItems=new ArrayList<String>();
于 2013-03-12T07:18:13.297 回答
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;
}
于 2013-03-12T07:19:15.457 回答