Java – ArrayAdapter requires the resource ID to be the TextView in the DialogFragment

ArrayAdapter requires the resource ID to be the TextView in the DialogFragment… here is a solution to the problem.

ArrayAdapter requires the resource ID to be the TextView in the DialogFragment

I use ArrayAdapter in DialogFragment, which works on Android 4.0 and later but not 2.3.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

View view = inflater.inflate(R.layout.dialog, null);

Spinner spReglement;
    spReglement = (Spinner)view.findViewById(R.id.listReglements);

ArrayAdapter<String> adapterList = new ArrayAdapter<String>(getActivity(), android. R.layout.simple_list_item_1);
    adapterList.setDropDownViewResource(R.layout.customspinner);
    spReglement.setAdapter(adapterList);

HashMap<Integer, String> mapReglement = new HashMap<Integer, String>();
    mapReglement.put(-1, "");
    adapterList.add("");
    for(int i=0; i<alReglement.size(); i++){
        String libelle = String.valueOf(alReglement.get(i).get("Libelle"));         
        mapReglement.put(i, libelle);
        adapterList.add(libelle);           
    }
    builder.setView(view)
           .setPositiveButton("Valider", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onDialogPositiveClick(DialogFSE.this);
               }
           })
           .setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onDialogNegativeClick(DialogFSE.this);
               }
           });      
    return builder.create();

}

My layout “

dialog” contains 3 EditText and 1 spinner and my layout “customspinner” contains only one TextView.

When I execute my app on Android 2.3, I get the following exception:
Fatal exception: com.mypackage.myapss.mainThread
java.lang.IllegalStateException: The ArrayAdapter requires a resource ID of TextView

Now don’t if it’s useful, but I used the android support v4 library in my app.

Solution

You need to use this ArrayAdapter constructor.

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects);

Edit:

ArrayAdapter<String> adapterList = new ArrayAdapter<String>(getActivity(),  R.layout.customspinnerregul, R.id.tvcustomspinner, al)

Related Problems and Solutions