如何在 Spinner 中连接两个数据库字段 (Android)

发布于 2024-12-22 09:24:27 字数 766 浏览 4 评论 0原文

我有一个数据库表,其中名字和姓氏位于两个单独的列中。我想在微调器上将它们显示为“史密斯,约翰”。我可以使用 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 技术交流群。

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

发布评论

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

评论(1

凉城已无爱 2024-12-29 09:24:27

我认为你需要在 newView 方法中膨胀你的布局。尝试这个

View view = View.inflate(context, android.R.layout.simple_spinner_dropdown_item, null);
return view;

然后在bindView中

TextView nameTextView = (TextView) view.findViewById(android.R.id.text1);
//the rest 

I think you need to inflate your layout in newView method. try this

View view = View.inflate(context, android.R.layout.simple_spinner_dropdown_item, null);
return view;

and then in bindView

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