Android:setListAdapter 仅设置数据库中的最后一行

发布于 2024-10-30 23:33:36 字数 1266 浏览 1 评论 0原文

正如标题所述,我在将数据从数据库链接到列表视图时遇到了一些麻烦,问题是适配器只会设置返回的最后一行,而不是数据库表中的每一行。

我想要实现的目标的一个例子:

餐桌动物: 猴 猫 狗 老虎

将在屏幕上显示为唯一的老虎。

我的方法从数据库返回光标:

public Cursor retrieveAnimals(){

    return = DB.query(ANIMAL_TABLE, new String[] {
            KEY_ROWID,
            KEY_ANIMALNAME,
            },
            null,
            null,
            null,
            null,
            null);      
}

并设置列表视图

    dbm = new MyDBManager(this);
    dbm.open();
    dbm.deleteAnimals();
    dbm.populateDB();

    // after populating, set the adapter..
    myCursor = dbm.getAllAnimals();

String[] columns = new String[] {"animal_name"};
int[] to = new int[] {R.id.animal};

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, myCursor, columns, to);
    this.setListAdapter(mAdapter);

我感觉我的问题出在光标位置,因为它在设置适配器时自动移动到最后一行,我一直在尝试找到问题的解决方案但没有运气。

预先感谢您的任何建议。

编辑:填充数据库的方法

public long populateDB(){


    ContentValues initialValues = new ContentValues();

    for(int i = 0; i < animalName.length; i++){
        initialValues.put(KEY_ANIMALNAME, animalName[i]);
    }

    return DB.insert(ANIMAL_TABLE, null, initialValues);

}

as the title states I am having a little trouble linking my data from a database to a listview, the problem being that the adapter will set only the last row returned, as opposed to each row from the database table.

An example of what I am trying to achieve:

TABLE ANIMAL:
Monkey
Cat
Dog
Tiger

will show up as only Tiger on screen.

My method returning cursor from the database:

public Cursor retrieveAnimals(){

    return = DB.query(ANIMAL_TABLE, new String[] {
            KEY_ROWID,
            KEY_ANIMALNAME,
            },
            null,
            null,
            null,
            null,
            null);      
}

and setting the listview

    dbm = new MyDBManager(this);
    dbm.open();
    dbm.deleteAnimals();
    dbm.populateDB();

    // after populating, set the adapter..
    myCursor = dbm.getAllAnimals();

String[] columns = new String[] {"animal_name"};
int[] to = new int[] {R.id.animal};

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, myCursor, columns, to);
    this.setListAdapter(mAdapter);

I have a feeling my problem lies in the cursor position, in that it is moving automatically to the last row when setting the adapter, I have been trying to find a solution to my problem but with no luck.

Thanks in advance for any suggestions.

EDIT: Method to populate DB

public long populateDB(){


    ContentValues initialValues = new ContentValues();

    for(int i = 0; i < animalName.length; i++){
        initialValues.put(KEY_ANIMALNAME, animalName[i]);
    }

    return DB.insert(ANIMAL_TABLE, null, initialValues);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-11-06 23:33:36

在您的 retrieveAnimals() 中,该行应该是 return DB.query(...) (请注意 = 之间缺少 = >返回和DB.query)。

除此之外,你的代码看起来不错。您应该在光标上调用 startManagingCursor(在调用 getAllAnimals 之后)以避免内存泄漏。

编辑:将您的填充方法更改为:

public long[] populateDB(){


    ContentValues initialValues = new ContentValues();
    long[] rowIds = new long[animalName.length];

    for(int i = 0; i < animalName.length; i++){
        // here we are using a single ContentValues object and inserting it
        // into the DB before changing the ContentValues and inserting it again
        initialValues.put(KEY_ANIMALNAME, animalName[i]);
        rowIds[i] = DB.insert(ANIMAL_TABLE, null, initialValues);
    }

    return rowIds;

}

或者(更多内存使用,但如果您需要在插入某些记录之前修改它们,则可能有用):

public void populateDB(){

    ContentValues[] initialValues = new ContentValues[animalName.length];

    for(int i = 0; i < animalName.length; i++){
        // create a new ContentValues for each object
        initialValues[i] = new ContentValues();
        initialValues[i].put(KEY_ANIMALNAME, animalName[i]);
    }

    // now our contentvalues is filled we can insert them all
    for(int i = 0; i < animalName.length; i++){
        DB.insert(ANIMAL_TABLE, null, initialValues[i]);
    }
}

In your retrieveAnimals(), the line should be return DB.query(...) (note the lack of an = between return and DB.query).

Apart from that your code looks fine. You should call startManagingCursor on the cursor (after calling getAllAnimals) to avoid memory leaks.

Edit: change your populate method to:

public long[] populateDB(){


    ContentValues initialValues = new ContentValues();
    long[] rowIds = new long[animalName.length];

    for(int i = 0; i < animalName.length; i++){
        // here we are using a single ContentValues object and inserting it
        // into the DB before changing the ContentValues and inserting it again
        initialValues.put(KEY_ANIMALNAME, animalName[i]);
        rowIds[i] = DB.insert(ANIMAL_TABLE, null, initialValues);
    }

    return rowIds;

}

Alternatively (more memory usage, but useful perhaps if you need to modify some of the records before inserting them):

public void populateDB(){

    ContentValues[] initialValues = new ContentValues[animalName.length];

    for(int i = 0; i < animalName.length; i++){
        // create a new ContentValues for each object
        initialValues[i] = new ContentValues();
        initialValues[i].put(KEY_ANIMALNAME, animalName[i]);
    }

    // now our contentvalues is filled we can insert them all
    for(int i = 0; i < animalName.length; i++){
        DB.insert(ANIMAL_TABLE, null, initialValues[i]);
    }
}
小鸟爱天空丶 2024-11-06 23:33:36

你尝试过吗

myCursor.moveToFirst()

Did you try

myCursor.moveToFirst()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文