我遇到自定义数组适配器的问题。我在堆栈上阅读了关于它的所有线程,仍然无法解决这个问题。我使用了这个问题的代码(它没有用,所以我做了一些小改动:Spinner with custom ArrayAdapter for objects not displaying selected item
问题是我创建了我的适配器并且: 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()值匹配。
答案 0 :(得分:0)
问题解决了。第一个错误是我有两个具有相同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谢谢你的帮助!