1

hey guys i can currently fetch data from database into a table but im stuck with listview. my code is this to fetch all data:

public ArrayList<Object> getRowAsArray(long rowID)
{
    // create an array list to store data from the database row.
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead.  That way you can ensure
    // data types are correct.
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {
        // this is a database call that creates a "cursor" object.
        // the cursor object store the information collected from the
        // database and is used to iterate through the data.
        cursor = db.query
        (
                TABLE_NAME,
                new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR, TABLE_ROW_FIVE, TABLE_ROW_SIX, TABLE_ROW_SEVEN },
                TABLE_ROW_ID + "=" + rowID,
                null, null, null, null, null
        );

        // move the pointer to position zero in the cursor.
        cursor.moveToFirst();

        // if there is data available after the cursor's pointer, add
        // it to the ArrayList that will be returned by the method.
        if (!cursor.isAfterLast())
        {
            do
            {
                rowArray.add(cursor.getLong(0));
                rowArray.add(cursor.getString(1));
                rowArray.add(cursor.getString(2));
                rowArray.add(cursor.getString(3));
                rowArray.add(cursor.getString(4));
                rowArray.add(cursor.getString(5));
                rowArray.add(cursor.getString(6));
                rowArray.add(cursor.getString(7));
            }
            while (cursor.moveToNext());
        }

        // let java know that you are through with the cursor.
        cursor.close();
    }
    catch (SQLException e) 
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList containing the given row from the database.
    return rowArray;
}

how would i populate this into a listview please. any help appreciated

1 Answer 1

2

It's much easier than that - you don't even need to create an array list.

The "Adapter" is the key - the adapter is the interface between the list and your data. In your case, you want a SimpleCursorAdapter. There are examples for that in the Api Demos. Check it out.

You just pass it the cursor of your query, and it will automatically populate your listview with the data.

Sign up to request clarification or add additional context in comments.

4 Comments

You know of any tutorial. I hav elooked but none to the type i needed. I have 7 rows in database to show
The API demos are very clearly structured. There is a group for "Lists", and in that there's one for "data from Cursor". Right here: developer.android.com/resources/samples/ApiDemos/src/com/…
thanks mate. i have just had a look now, they seem to be using it for uris. Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, NoteColumns.DEFAULT_SORT_ORDER); // Used to map notes entries from the database to views SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor, new String[] { NoteColumns.TITLE }, new int[] { android.R.id.text1 }); setListAdapter(adapter); how would mine link to this. thanks mate
Same way. A cursor is a cursor. They use a ContentProvider, which is an abstraction of a database, you use the database directly. Doesn't matter. The final result is a Cursor, and that's what you plug into the SimpleCursorAdapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.