Android 从数据库填充listview

Android 从数据库填充listview,android,database,listview,arraylist,Android,Database,Listview,Arraylist,嘿,伙计们,我现在可以从数据库中提取数据到一个表中,但我仍然坚持使用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 //

嘿,伙计们,我现在可以从数据库中提取数据到一个表中,但我仍然坚持使用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;
}
public ArrayList getRowAsArray(长rowID)
{
//创建数组列表以存储数据库行中的数据。
//我建议创建一个与JavaBean兼容的对象
//而是存储此数据。这样可以确保
//数据类型是正确的。
ArrayList rowArray=新的ArrayList();
光标;
尝试
{
//这是一个创建“游标”对象的数据库调用。
//游标对象存储从
//数据库,用于遍历数据。
cursor=db.query
(
表格名称,
新字符串[]{TABLE_ROW_ID,TABLE_ROW_ONE,TABLE_ROW_TWO,TABLE_ROW_三,TABLE_ROW_四,TABLE_ROW_五,TABLE_ROW_六,TABLE_ROW_七},
表_行_ID+“=”+rowID,
空,空,空,空,空,空
);
//将指针移动到光标中的零位置。
cursor.moveToFirst();
//如果光标指针后有可用数据,请添加
//它将发送到ArrayList,该ArrayList将由方法返回。
如果(!cursor.isAfterLast())
{
做
{
添加(cursor.getLong(0));
添加(cursor.getString(1));
添加(cursor.getString(2));
添加(cursor.getString(3));
添加(cursor.getString(4));
添加(cursor.getString(5));
添加(cursor.getString(6));
添加(cursor.getString(7));
}
while(cursor.moveToNext());
}
//让java知道您已经完成了光标操作。
cursor.close();
}
捕获(SQLE异常)
{
Log.e(“DB ERROR”,e.toString());
e、 printStackTrace();
}
//从数据库返回包含给定行的ArrayList。
返回行数组;
}

我如何将其填充到listview中。感谢您的任何帮助

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

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


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

您知道任何教程。我已经私奔了,但没有达到我所需要的类型。我在数据库中有7行来显示API演示的结构非常清晰。有一组用于“列表”,其中有一组用于“来自游标的数据”。就在这里:谢谢你,伙计。我刚刚看了一下,他们似乎在用它来做URI。Cursor Cursor=managedQuery(getIntent().getData(),PROJECTION,null,null,NoteColumns.DEFAULT_SORT_ORDER);//用于将数据库中的notes条目映射到视图SimpleCursorAdapter=new SimpleCursorAdapter(this,R.layout.noteslist_项,游标,新字符串[]{NoteColumns.TITLE},新int[]{android.R.id.text1});setListAdapter(适配器);我将如何与此联系。同样谢谢你。光标就是光标。他们使用ContentProvider,这是数据库的抽象,您可以直接使用数据库。没关系。最终的结果是一个游标,这就是您插入SimpleCursorAdapter的内容。