我在 DialogFragment 中使用 ArrayAdapter,它适用于 Android 4.0 及更高版本但不适用于 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 个微调器,我的布局“customspinner”仅包含一个 TextView。
当我在 Android 2.3 上执行我的应用程序时,出现以下异常: 致命异常:com.mypackage.myapss.mainThread java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView ...
现在如果它有用就不要,但我在我的应用程序中使用了 android 支持 v4 库。
最佳答案
您需要使用此 ArrayAdapter 构造函数。
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects);
编辑:
ArrayAdapter<String> adapterList = new ArrayAdapter<String>(getActivity(), R.layout.customspinnerregul, R.id.tvcustomspinner, al)
关于java - ArrayAdapter要求资源ID为DialogFragment中的TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20762345/