简体   繁体   English

将数据从游标绑定到Spinner Android

[英]Binding data from a Cursor to a Spinner Android

I am trying to populate a Spinner in an Android dialog. 我正在尝试在Android对话框中填充微调框。 When the user clicks a button, a dialog pops up and I load the layout from an XML file. 当用户单击按钮时,将弹出一个对话框,我从XML文件加载布局。 Now I am trying to populate that Spinner from a SQL query. 现在,我正在尝试通过SQL查询填充该Spinner。 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. 我可以遍历Cursor,并将每个值添加到ArrayAdapter,然后将其用作微调框的列表,但这与数据库中的_id无关。 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. 在使用SimpleCursorAdapter之前,我已经完成了此操作,我什至完全复制并粘贴了旧代码,但仍然无法正常工作。 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: 这是在我的DatabaseInterface类中返回Cursor的代码:

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. 我知道我可以使用上面提到的ArrayAdapter的名称列表加载Spinner,还可以使用查询中的ID填充数组,并且当用户选择一个项目时,我可以从ID的数组中获取相应的项...但是我知道SimpleCursorAdapter可以工作,但我似乎无法弄清楚,直到哈哈我才放弃。 Thanks in advance for any help. 在此先感谢您的帮助。

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(); 我刚刚删除了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. 像往常一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM