spences10
spences10

Reputation: 590

Populate ListView from SQLite db

I'm trying to populate a list view from a SQLite db I can create the db and add items to it and display them in a TextView but for some reason not on a ListView

Is it that sData is the wrong type of object?

Can anyone help, please?

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

Upvotes: 1

Views: 1674

Answers (3)

Tom Leese
Tom Leese

Reputation: 19679

You will end up with a ListView with just one single item, the value of sData. You need to create a list such as:

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

I also recommend you change your cursor looping to be similar to this:

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

because at the moment you are not checking the return value of moveToFirst, and it may return false (meaning there are no rows), however your do-while loop means the cursor will be read at least once whether or not it has 0 rows, and if there are 0 rows, you app will crash.

Upvotes: 4

user1868632
user1868632

Reputation: 253

sData is no array try something like

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

Upvotes: 1

mango
mango

Reputation: 5636

String sData="";

try to make sData a String array. feed that into the adapter.

Upvotes: 1

Related Questions