spinner getView()的自定义阵列adapter未调用



我对自定义数组适配器有问题。我阅读了有关它的所有线程,但仍然无法解决此问题。我使用了这个问题中的代码(而且它不起作用,所以我做了一些小更改:带有自定义阵列的旋转器,用于未显示选定项目

的对象

问题是我创建适配器,并且: 1. GetCount显示正确的价值(244( 2. getView没有被称为 3. GetDropdown视图也未调用。

您能帮我解决这个问题吗?

适配器类:

public class DishesFilterCustomArrayAdapter extends ArrayAdapter<Dish> {
private List<Dish> items;
private Context context;
private Activity activity;
public DishesFilterCustomArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Dish> objects) {
    super(context, resource, textViewResourceId, objects);
    this.items = objects;
    this.context = context;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    if (v == null) {
        v = new TextView(context);
    }
    v.setTextColor(context.getResources().getColor(R.color.blue_light));
    v.setText(items.get(position).dishName);
    return v;
}
@Override
public Dish getItem(int position) {
    return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        v = inflater.inflate(R.layout.payment_mode_payer_item, null);
    }
    TextView lbl = (TextView) v.findViewById(R.id.textViewForAdapter);
    lbl.setTextColor(context.getResources().getColor(R.color.blue_light));
    lbl.setText(items.get(position).dishName);
    return convertView;
}
@Override
public int getCount() {
    return items.size();
}
public List<Dish> getItems() {
    return items;
}

片段中的代码:

        mDishList = new ArrayList<>();
        mDishList.addAll(getAllDishesSegregatedList(getAllDishesList()));
        mDishesAdapter = new DishesFilterCustomArrayAdapter(getContext(), R.layout.payment_mode_payer_item, R.id.textViewForAdapter, mDishList);
        //mDishesAdapter.add(new Dish(getString(R.string.filter_receipt_history_by_dish_name)));
        dishesFilterSpinner.setAdapter(mDishesAdapter);

mdishlist大小为244,并且与getCount((值匹配。

解决了问题。第一个错误是我有两个具有相同ID的视图,我将适配器设置为一个不可见的转移。

第二是我在自定义适配器的代码中犯了一些错误。我在下面发布。

public class DishesFilterCustomArrayAdapter extends ArrayAdapter<Dish> {
private List<Dish> items;
private Context context;
public DishesFilterCustomArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Dish> objects) {
    super(context, resource, textViewResourceId, objects);
    this.items = objects;
    this.context = context;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        v = inflater.inflate(R.layout.receipt_history_spinner_item, null);
    }
    TextView lbl = (TextView) v.findViewById(R.id.receiptHistorySpinnerItemTextView);
    lbl.setTextColor(context.getResources().getColor(R.color.blue_light));
    lbl.setText(items.get(position).dishName);
    return v;
}
@Override
public Dish getItem(int position) {
    return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        v = inflater.inflate(R.layout.receipt_history_spinner_item, null);
    }
    TextView lbl = (TextView) v.findViewById(R.id.receiptHistorySpinnerItemTextView);
    lbl.setTextColor(context.getResources().getColor(R.color.blue_light));
    lbl.setText(items.get(position).dishName);
    return v;
}
@Override
public int getCount() {
    return items.size();
}
public List<Dish> getItems() {
    return items;
}

Mike.m感谢您的帮助!

最新更新