将数据从 Cursor 绑定到 Spinner Android

发布于 2024-11-27 08:26:58 字数 1621 浏览 1 评论 0原文

我正在尝试在 Android 对话框中填充 Spinner。当用户单击按钮时,会弹出一个对话框,然后我从 XML 文件加载布局。现在我尝试从 SQL 查询填充 Spinner。我已经搜索遍了,但无法弄清楚问题出在哪里。我可以循环遍历 Cursor 并将每个值添加到 ArrayAdapter,然后将其用作微调器的列表,但它不附带数据库中的 _id。我在使用 SimpleCursorAdapter 之前已经完成了此操作,我什至准确地复制并粘贴了我的旧代码,但它仍然无法工作。任何帮助将不胜感激。

我的对话框代码:

private void displayNewInteractionDialog() {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.mm_new_dialog);
        dialog.setTitle(R.string.mm_new_dialog_title);
        dialog.setCancelable(true);

        final Spinner spinner = (Spinner) dialog.findViewById(R.id.mm_spinner);

        DatabaseInterface db = new DatabaseInterface(this);
        db.open();

        Cursor c = db.getNames();

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_spinner_item,
            c,
            new String[] {DatabaseInterface.KEY_ID, DatabaseInterface.KEY_NAME}, 
            new int[] {android.R.id.text1});

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        c.close();
        db.close();

        dialog.show();
}

这是在我的 DatabaseInterface 类中返回游标的代码:

public Cursor getNames() {
        return db.query(DATABASE_TABLE_4, new String[] {DatabaseInterface.KEY_ID, DatabaseInterface.KEY_NAME}, null, null, null, null, null);
    }

我知道这会起作用,我只是显然错过了一些东西。我知道我可以使用上面提到的 ArrayAdapter 中的名称列表加载 Spinner,还可以使用查询中的 ID 填充一个数组,当用户选择一项时,我可以从 ID 数组中获取相应的项...但我知道 SimpleCursorAdapter 会起作用,我只是似乎无法弄清楚,直到我明白为止我不会放弃哈哈。预先感谢您的任何帮助。

I am trying to populate a Spinner in an Android dialog. When the user clicks a button, a dialog pops up and I load the layout from an XML file. Now I am trying to populate that Spinner from a SQL query. I have searched all over and cannot figure out what the problem is. I can loop through the Cursor and add each value to an ArrayAdapter and then use that as the list for the spinner but that doesn't come with the _id's from the database. I have done this before using a SimpleCursorAdapter and I have even copied and pasted my old code exactly and still it isn't working. Any help would be much appreciated.

My Dialog code:

private void displayNewInteractionDialog() {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.mm_new_dialog);
        dialog.setTitle(R.string.mm_new_dialog_title);
        dialog.setCancelable(true);

        final Spinner spinner = (Spinner) dialog.findViewById(R.id.mm_spinner);

        DatabaseInterface db = new DatabaseInterface(this);
        db.open();

        Cursor c = db.getNames();

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_spinner_item,
            c,
            new String[] {DatabaseInterface.KEY_ID, DatabaseInterface.KEY_NAME}, 
            new int[] {android.R.id.text1});

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        c.close();
        db.close();

        dialog.show();
}

Here is the code that returns a Cursor in my DatabaseInterface class:

public Cursor getNames() {
        return db.query(DATABASE_TABLE_4, new String[] {DatabaseInterface.KEY_ID, DatabaseInterface.KEY_NAME}, null, null, null, null, null);
    }

I know this will work I am just missing something apparently. I know that I could load the Spinner with the list of names from the ArrayAdapter that I stated above and also populate an array with the ID's from the query and when the user selects an item I could just grab the corresponding one from the array of ID's... But I know the SimpleCursorAdapter will work I just can't seem to figure it out and I'm not giving up until I do haha. Thanks in advance for any help.

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

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

发布评论

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

评论(1

静谧 2024-12-04 08:26:58

好吧,看起来我很蠢哈哈......出于某种原因,可能是因为尝试了很多事情然后忘记删除我添加的部分。我刚刚删除了 c.close(); 现在一切都很好。我记得在我试图解决问题时添加了这一点,我必须在其他地方解决问题,然后没有意识到这一点,因为这条线仍然在那里。由于除了将 Cursor 传递给函数之外,我实际上没有对 Cursor 执行任何操作,因此不需要关闭它。无论如何,一切都很好,我知道这将是一件愚蠢的事情。像往常一样。

Well looks like I am dumb haha... For some reason or another, probably from just trying numerous things and then forgetting to remove parts that I had added. I just removed c.close(); and now everything is fine. I remember adding that as I was trying to figure things out and I must've fixed the problem somewhere else and then didn't realize it because this line was still in there. Since I am not actually doing anything with the Cursor other than passing it to a function, it doesn't need to be closed. Anyhow all is good, I knew it was going to be something stupid. Like usual.

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