rainman rainman - 2 months ago 12
Java Question

Error setting adapter to spinner in Android

I am trying to set an adapter to a spinner but I don't know why I am getting the following error:
Cannot resolve constructor 'ArrayAdapter(com.example.app.DialogBox,int, java.util.list)'

here is the method where I try to populate the spinner and set the adapter for it:

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;

}
}


And this is the 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"/>


Any help please ?

Answer

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() method of Fragment.