SQLite для ListActivity - PullRequest
       4

SQLite для ListActivity

0 голосов
/ 20 сентября 2010

У меня есть этот код в моем помощнике базы данных:

public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
    // create an ArrayList that will hold all of the data collected from
    // the database.
    ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();

    // 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 cursor;

    try
    {
        // ask the database object to create the cursor.
        cursor = db.query(
                TABLE_NAME,
                new String[]{TABLE_ROW_NAME},
                null, null, null, null, null
        );

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

        // if there is data after the current cursor position, add it
        // to the ArrayList.
        if (!cursor.isAfterLast())
        {
            do
            {
                ArrayList<Object> dataList = new ArrayList<Object>();

                dataList.add(cursor.getString(1));

                dataArrays.add(dataList);
            }
            // move the cursor's pointer up one position.
            while (cursor.moveToNext());
        }
    }
    catch (SQLException e)
    {
        Log.e("DB Error", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList that holds the data collected from
    // the database.
    return dataArrays;
}

Как мне использовать этот код и показать результаты в ListActivity?

Застрял в этом за последние 2 дня. Любая помощь по этому вопросу приветствуется.

Заранее спасибо.

1 Ответ

2 голосов
/ 20 сентября 2010

Вам необходимо расширить SimpleCursorAdapter и заставить его возвращать вложенные представления для строк. Вы можете использовать положение строки, которую ListView запрашивает как индекс в строке ваших результатов SQL.

Смотрите это видео о настройке ListView.

...