ArrayAdapter要求资源ID为DialogFragment中的TextView [英] ArrayAdapter requires the resource ID to be a TextView in DialogFragment
问题描述
我在DialogFragment中使用了ArrayAdapter,它可以在Android 4.0及更高版本上运行,但不能在2.3中运行.
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();
}
我的布局对话框"包含3个EditText和1个Spinner,我的布局"customspinner"仅包含一个TextView.
My Layout "dialog" contains 3 EditText and 1 Spinner, my layout "customspinner" constains only a TextView.
当我在Android 2.3上执行我的应用程序时,出现以下异常:致命异常:com.mypackage.myapss.mainThreadjava.lang.IllegalStateException:ArrayAdapter要求资源ID为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 ...
现在就不用了,但是我在我的应用程序中使用了android support v4库.
Don't now if it's useful but I use the android support v4 library in my app.
推荐答案
您需要使用此ArrayAdapter构造函数.
You need to use this ArrayAdapter Constructor.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects);
ArrayAdapter<String> adapterList = new ArrayAdapter<String>(getActivity(), R.layout.customspinnerregul, R.id.tvcustomspinner, al)
这篇关于ArrayAdapter要求资源ID为DialogFragment中的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!