代码之家  ›  专栏  ›  技术社区  ›  Jan Bej-Radzikowski

未调用微调器getView()的自定义ArrayAdapter

  •  2
  • Jan Bej-Radzikowski  · 技术社区  · 7 年前

    自定义阵列适配器有问题。我阅读了堆栈上的所有线程,但仍然无法解决此问题。我使用了这个问题中的代码(它不起作用,所以我做了一些小改动: 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()值匹配。

    1 回复  |  直到 7 年前
        1
  •  4
  •   Jan Bej-Radzikowski    7 年前

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