从SQLite db填充ListView

时间:2013-01-06 19:01:15

标签: android android-listview

我正在尝试从SQLite数据库填充列表视图我可以创建数据库并向其添加项目并在TextView中显示它们但由于某种原因不在ListView上

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();
        }
    }

3 个答案:

答案 0 :(得分: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行,你的应用程序将崩溃。

答案 1 :(得分:1)

String sData="";

尝试将sData设为字符串数组。将其送入适配器。

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