创建 ArrayAdapter 时出错
Error creating ArrayAdapter
我正在尝试将适配器设置为微调器,但我不知道为什么会出现以下错误:
Cannot resolve constructor 'ArrayAdapter(com.example.app.DialogBox,int, java.util.list)'
这是我尝试填充微调器并为其设置适配器的方法:
public class DialogBox extends DialogFragment implements View.OnClickListener {
// To fill the age spinner
public ArrayAdapter<Integer> populateAgeSpinner () {
Log.d(TAG ,"populateAgeSpinner - Ini");
List age = new ArrayList<String>();
age.add("");
for(int i = 18; i <= 100; i++) {
age.add(Integer.toString(i));
}
ArrayAdapter<Integer> spinnerAgeAdapter = new ArrayAdapter<Integer>(this,R.layout.spinner_item_layout, age);
spinnerAgeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d(TAG ,"populateAgeSpinner - Fi");
return spinnerAgeAdapter;
}
}
这是 spinner_item_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_text"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="30dp"
android:ellipsize="marquee"
android:background="@color/com_facebook_button_send_background_color"
android:textColor="#000"
android:textAlignment="inherit"/>
DialogFragment does not extend Context
needed for each of ArrayAdapter constructors. You should use Context
or any of its inheritors in the constructor. To get Context
call the getActivity()
Fragment
的方法。
我正在尝试将适配器设置为微调器,但我不知道为什么会出现以下错误:
Cannot resolve constructor 'ArrayAdapter(com.example.app.DialogBox,int, java.util.list)'
这是我尝试填充微调器并为其设置适配器的方法:
public class DialogBox extends DialogFragment implements View.OnClickListener {
// To fill the age spinner
public ArrayAdapter<Integer> populateAgeSpinner () {
Log.d(TAG ,"populateAgeSpinner - Ini");
List age = new ArrayList<String>();
age.add("");
for(int i = 18; i <= 100; i++) {
age.add(Integer.toString(i));
}
ArrayAdapter<Integer> spinnerAgeAdapter = new ArrayAdapter<Integer>(this,R.layout.spinner_item_layout, age);
spinnerAgeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d(TAG ,"populateAgeSpinner - Fi");
return spinnerAgeAdapter;
}
}
这是 spinner_item_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_text"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="30dp"
android:ellipsize="marquee"
android:background="@color/com_facebook_button_send_background_color"
android:textColor="#000"
android:textAlignment="inherit"/>
DialogFragment does not extend Context
needed for each of ArrayAdapter constructors. You should use Context
or any of its inheritors in the constructor. To get Context
call the getActivity()
Fragment
的方法。