SQLite到ListActivity

时间:2010-09-20 11:10:03

标签: android sqlite listactivity

我在DataBase Helper中有这个代码:

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 个答案:

答案 0 :(得分:2)

您需要扩展SimpleCursorAdapter并让它返回行的子视图。您可以使用ListView请求的行的位置作为SQL结果行的索引。

Watch this video on customizing ListView.