【问题标题】:How to concatenate two DB fields in a Spinner (Android)如何在 Spinner (Android) 中连接两个数据库字段
【发布时间】:2012-03-07 19:10:14
【问题描述】:

我有一个数据库表,其中 first_name 和 last_name 在两个单独的列中。我想在 Spinner 上将它们显示为“Smith, John”。我可以使用 SimpleCursorAdapter 毫无问题地显示其中一个名称。我想为了显示这两列,我必须扩展 CursorAdapter。我有这个工作正常,但我不确定是否需要为微调器制作自定义布局?我能找到的每个 CustomAdapter 示例都使用列表视图,而不是微调器。这是我在 CustomAdapater 的 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。如何访问微调器上的文本字段?

谢谢。

【问题讨论】:

标签: android spinner adapter


【解决方案1】:

我认为您需要在 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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    相关资源
    最近更新 更多