Question

I use an ArrayAdapter in a DialogFragment, that work on Android 4.0 and more but not in 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, my layout "customspinner" constains only a TextView.

When I execute my app on Android 2.3 I have the following exception : FATAL EXCEPTION : com.mypackage.myapss.mainThread java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView ...

Don't now if it's useful but I use the android support v4 library in my app.

Was it helpful?

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top