使用数据库查询填充微调器

发布于 2024-11-28 12:07:44 字数 713 浏览 1 评论 0原文

我最近开始进行 android 和 java 开发的编程。目前我通过编写时间表应用程序进行训练。 我想从数据库中获取包含所有主题的列表(该表还包含教师和房间)并将它们放入旋转器中。 这就是我编写的代码

Cursor c = dba.fetchAllSubjects();

if (c.getCount() != 0)  {
    Spinner subjectSpinner = (Spinner) findViewById(R.id.newlesson_subject);
    startManagingCursor(c);
    String[] from = new String[]{DbAdapter.KEY_SUBJECT}; 
    int[] to = new int[] {android.R.layout.simple_spinner_item};
    SimpleCursorAdapter subjectAdapter = 
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
    subjectSpinner.setAdapter(subjectAdapter);
}

问题是,当单击微调器时,它会显示一个列表,其中包含与数据库条目一样多的项目,但该列表不显示名称(您可以说它是空的)。所以我可能把简单的光标适配器搞砸了,但我不知道是什么。

感谢您的帮助

I recently started programming for android and java development in general. Currently I train by writing a timetable app.
I want to get a list with all subjects from the db (the table also contains teacher and rooms) and put them into a spinner.
Thats the code I wrote

Cursor c = dba.fetchAllSubjects();

if (c.getCount() != 0)  {
    Spinner subjectSpinner = (Spinner) findViewById(R.id.newlesson_subject);
    startManagingCursor(c);
    String[] from = new String[]{DbAdapter.KEY_SUBJECT}; 
    int[] to = new int[] {android.R.layout.simple_spinner_item};
    SimpleCursorAdapter subjectAdapter = 
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
    subjectSpinner.setAdapter(subjectAdapter);
}

The problem is that when clicking on the spinner it shows a list with as much items as the db has entries, but the list doesn't show the names (you can say it's empty). So I probably messed something up with the simple cursor adapter, but I don't know what.

Thanks for your help

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

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

发布评论

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

评论(1

梦归所梦 2024-12-05 12:07:44

您的错误在于在 to 整数数组中指定了错误的资源 ID。

int[] to = new int[] {android.R.layout.simple_spinner_item};

您需要指定 TextView(或类似类型的视图)的 ID,而不是布局本身。将上面的行替换为:

int[] to = new int[] { android.R.id.text1 };

You're error lies in specifying the wrong Resource ID in your to integer array.

int[] to = new int[] {android.R.layout.simple_spinner_item};

You need to specify the ID of a TextView (or similarly typed view) and not the layout itself. Replace the line above with:

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