使用数据库查询填充微调器
我最近开始进行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误在于在 to 整数数组中指定了错误的资源 ID。
您需要指定 TextView(或类似类型的视图)的 ID,而不是布局本身。将上面的行替换为:
You're error lies in specifying the wrong Resource ID in your to integer array.
You need to specify the ID of a TextView (or similarly typed view) and not the layout itself. Replace the line above with: