0
public class MakePaymentCustomView extends LinearLayout {
    public MakePaymentCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.make_payment_custom_layout, this, true);
        String title;
        String subtitle;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PaymentCustomView, 0, 0);
        try {
            title = a.getString(R.styleable.PaymentCustomView_customViewTitle);
            subtitle = a.getString(R.styleable.PaymentCustomView_customViewSubtitle);
        } finally {
            a.recycle();
        }
        // Throw an exception if required attributes are not set
        if (title == null) {
            throw new RuntimeException("No title provided");
        }
        if (subtitle == null) {
            throw new RuntimeException("No subtitle provided");
        }
        init(title, subtitle);
    }
    // Setup views
    private void init(String title, String subtitle) {
        List<String> categories = new ArrayList<String>();
        categories.add("Automobile");
        categories.add("Business Services");
        categories.add("Computers");
        categories.add("Education");
        categories.add("Personal");
        categories.add("Travel");
        TextView titleView = findViewById(R.id.customview_textview_title);
        TextView subtitleView = findViewById(R.id.customview_textview_subtitle);
        Spinner voucherList = findViewById(R.id.voucherSpinner);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(), voucherList, categories);
        voucherList.setAdapter(dataAdapter);
        titleView.setText(title);
        subtitleView.setText(subtitle);
    }
}

I am trying to set the Array Adapter into the spinner, however I am unable to do so due the following error:

Cannot resolve constructor ArrayAdapter

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
kylas
  • 1,425
  • 6
  • 24
  • 38

2 Answers2

2

You don't need activity to create ArrayAdapter.

Just create the adapter like this:

ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, categories);
Mahmoud Elshamy
  • 578
  • 1
  • 5
  • 13
-1

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(), voucherList, categories);You are doing it wrong. please check documentation below https://developer.android.com/reference/android/widget/ArrayAdapter#public-constructors
to get context voucherList.getContext()

Amit Goswami
  • 314
  • 2
  • 11