Search code examples
androidandroid-arrayadapterandroid-spinner

Custom ArrayAdapter for Spinner getView() not called


I'm having a problem with custom array adapter. I read all threads on stack about it and still can't solve this issue. I used the code from this question (and it was not working so I made some small changes: Spinner with custom ArrayAdapter for objects not displaying selected item

The problem is that I create my adapter and: 1. getCount shows proper value (244) 2. getView is not being called 3. getDropdown view not called too.

Can you please help me out with this one?

Adapter class:

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;
}

code from a fragment:

        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 size is 244 and it matches getCount() value.


Solution

  • Problem solved. First mistake was that I had two views with same id and I set my adapter to the one which not visible.

    Second was that I had some mistakes in code of my custom adapter. I post it below.

    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 thanks you for your help!