问题已解决。第一个错误是,我有两个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;
}
迈克。M谢谢你的帮助!