【问题标题】:Populate listview from database从数据库填充列表视图
【发布时间】:2010-12-05 21:57:57
【问题描述】:

嘿伙计们,我目前可以将数据从数据库中提取到表中,但我坚持使用 listview。我的代码是这样获取所有数据的:

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

请我如何将其填充到列表视图中。任何帮助表示赞赏

【问题讨论】:

    标签: android database listview arraylist


    【解决方案1】:

    比这容易得多 - 您甚至不需要创建数组列表。

    “适配器”是关键 - 适配器是列表和数据之间的接口。在您的情况下,您需要SimpleCursorAdapter。 Api Demos 中有这方面的例子。看看吧。

    您只需将查询的光标传递给它,它就会自动用数据填充您的列表视图。

    【讨论】:

    • 你知道任何教程。我已经看过但没有我需要的类型。我在数据库中有 7 行要显示
    • API 演示结构非常清晰。有一个“列表”组,其中有一个“来自光标的数据”。就在这里:developer.android.com/resources/samples/ApiDemos/src/com/…
    • 谢谢伙计。我现在刚刚看了看,他们似乎将它用于uris。光标 cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, NoteColumns.DEFAULT_SORT_ORDER); // 用于将笔记条目从数据库映射到视图 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor, new String[] { NoteColumns.TITLE }, new int[] { android.R.id.text1 }); setListAdapter(适配器);我的链接如何。谢谢队友
    • 同理。游标就是游标。他们使用 ContentProvider,它是数据库的抽象,您直接使用数据库。没关系。最终结果是一个游标,这就是您插入 SimpleCursorAdapter 的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多