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);
}
}
我正在尝试将阵列适配器设置为微调器,但是由于以下错误,我无法这样做:
无法解析构造函数ArrayAdapter
答案 0 :(得分:2)
您无需进行活动即可创建ArrayAdapter。
只需这样创建适配器:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, categories);
答案 1 :(得分:-1)
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(), voucherList, categories);
您做错了。
请检查下面的文档
https://developer.android.com/reference/android/widget/ArrayAdapter#public-constructors
获取上下文voucherList.getContext()