【问题标题】:Android custom ArrayAdapter rendering Objects in list, not valuesAndroid自定义ArrayAdapter在列表中呈现对象,而不是值
【发布时间】:2018-08-19 16:18:10
【问题描述】:

我正在尝试将自定义布局(包含两个 TextView 的 LinearLayout)绑定到 Spinner。我正确地对 ArrayAdapter 进行了子类化(大部分)。在 Spinner 中选择一个项目正确调用 getView(),正确设置 LinearLayout 的 TextViews 值。问题是微调器中项目的初始显示(单击微调器时)只显示对象;不是他们应该显示的 TextViews。只有在单击其中一个对象之后,Spinner 才会使用我的自定义适配器的 getView() 方法正确设置 TextView。这是自定义适配器类:

public class BusRouteAdapter extends ArrayAdapter<BusRoute> {
...
public BusRouteAdapter(Context context, int resource, int textViewId, ArrayList<BusRoute> routes) {
    super(context, resource, textViewId, routes);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    BusRoute route = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_route, parent, false);
    }
    TextView tvBusRoute = (TextView) convertView.findViewById(R.id.tvBusRoute);
    TextView tvBusRouteNumber = (TextView) convertView.findViewById(R.id.tvBusRouteNumber);
    tvBusRoute.setText(route.routeName); 
    tvBusRoute.setTag(route.route);
    tvBusRouteNumber.setText(route.route);
    if (!route.routeColor.equals("")) {
        tvBusRouteNumber.setBackgroundColor(Color.parseColor(route.routeColor));
    }
    return convertView;
}
}

这是每个 Spinner 列表项 (bus_route.xml) 使用的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/tvBusRouteNumber"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:padding="8dp" />

    <TextView
        android:id="@+id/tvBusRoute"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:padding="8dp" />
</LinearLayout>

在我的 Activity 中,我将适配器设置为正确填充的 BusRoute 对象列表:

busRouteAdapter = new BusRouteAdapter(getBaseContext(), R.layout.bus_route, R.id.tvBusRoute, arrayOfBusRoutes);
        routesSpinner.setAdapter(busRouteAdapter);

我需要传递布局 id (R.layout.bus_route) 和包含在该布局 (R.id.tvBusRoute) 中的 TextView 之一,这似乎很奇怪。

这是单击 Spinner 时呈现的内容:

但是,如果我单击其中一个对象,则会调用 getView(),并且正确呈现所选的 Layout 和 TextViews(显然我选择了“GMU - Pentagon”):

为了让 Spinner 的弹出列表显示正确渲染的所有公交路线项目,我缺少什么?

【问题讨论】:

  • 您没有覆盖getDropDownView(),因此,默认情况下,它会显示您在构造函数中传递的TextView 中的BusRoute 对象的toString() 返回值。你显然没有在BusRoute 中覆盖toString(),所以这就是你在那里得到Object toString() 的原因。
  • @mike-m 啊,对。那么当我在 BusRoute 中重写 toString() 时,由于我需要解析 BusRoute 对象以显示在不同的 TextView 中,如何指定有意义的结果?我假设我可以显示我在 Spinner 的弹出列表中定义的格式丰富的 TextView?...
  • 覆盖toString() 不会解决您的整个问题。这只是为了解释为什么您会在下拉项目中看到它。您需要在getDropDownView() 中处理您的项目View,就像您在getView() 中一样。事实上,如果你想让它们看起来完全一样,你可以只在 getDropDownView() 中使用 return getView(position, convertView, parent);
  • @MikeM。祝福你。就是这样。我用谷歌搜索了这个地狱,但我没有看到对覆盖 getDropDownView() 的引用。我这样做了,相应地设置了值,它正在工作。该死,这很简单。谢谢谢谢谢谢。

标签: android android-layout spinner android-arrayadapter android-spinner


【解决方案1】:

我认为你需要重写 getDropDownView 来处理微调器

  @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
             BusRoute route = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_route, parent, false);
    }
    TextView tvBusRoute = (TextView) convertView.findViewById(R.id.tvBusRoute);
    TextView tvBusRouteNumber = (TextView) convertView.findViewById(R.id.tvBusRouteNumber);
    tvBusRoute.setText(route.routeName); 
    tvBusRoute.setTag(route.route);
    tvBusRouteNumber.setText(route.route);
    if (!route.routeColor.equals("")) {
        tvBusRouteNumber.setBackgroundColor(Color.parseColor(route.routeColor));
    }
    return convertView;

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多