问题
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.
回答1:
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)
来源:https://stackoverflow.com/questions/20762345/arrayadapter-requires-the-resource-id-to-be-a-textview-in-dialogfragment