如何在 Spinner 中连接两个数据库字段 (Android)
我有一个数据库表,其中名字和姓氏位于两个单独的列中。我想在微调器上将它们显示为“史密斯,约翰”。我可以使用 SimpleCursorAdapter 毫无问题地显示其中一个名称。我想为了显示两列,我必须扩展 CursorAdapter。我可以正常工作,但我不确定是否需要为微调器制作自定义布局?我能找到的每个 CustomAdapter 示例都使用列表视图,而不是微调器。这是我在 CustomAdapter 的 bindView 方法中的内容:
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTextView = (TextView) view.findViewById(android.R.layout.simple_spinner_dropdown_item);
String first = cursor.getString(cursor.getColumnIndex(Hunter.FIRST_NAME));
String last = cursor.getString(cursor.getColumnIndex(Hunter.LAST_NAME));
nameTextView.setText(last + ", " + first);
}
显然,这不起作用,因为传入的视图不包含 android.R.layout.simpler_spinner_dropdown_item,因此 nameTextView 为 null。如何访问微调器上的文本字段?
谢谢。
I have a database table with first_name and last_name in two separate columns. I want to display them as "Smith, John" on a Spinner. I can show one of the names with no problem using a SimpleCursorAdapter. I thought in order to show both columns, I would have to extend CursorAdapter. I have this working ok, but I am not sure if I need to make a custom layout for the spinner? Every CustomAdapter example that I can find is using a list view, not a spinner. Here is what I have in my bindView method of the CustomAdapater:
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTextView = (TextView) view.findViewById(android.R.layout.simple_spinner_dropdown_item);
String first = cursor.getString(cursor.getColumnIndex(Hunter.FIRST_NAME));
String last = cursor.getString(cursor.getColumnIndex(Hunter.LAST_NAME));
nameTextView.setText(last + ", " + first);
}
Obviously, this does not work because the view passed in does not contain android.R.layout.simpler_spinner_dropdown_item, so nameTextView is null. How do I get at the text field on a spinner?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你需要在 newView 方法中膨胀你的布局。尝试这个
然后在bindView中
I think you need to inflate your layout in newView method. try this
and then in bindView