我正在尝试在(Sherlock)ArrayAdapter
的{{1}}方法中为ListView
设置onCreateDialog()
。我刚刚得到DialogFragment
,但我认为布局和字符串数组的路径是正确的,因为当我直接为NullPointerException
设置适配器时,会显示List。
AlertDialog.Builder
有什么建议吗?
编辑:
事实证明,行条目XML似乎不正确,因为我用
测试了下面提到的代码public class InputMyDialogFragment
extends SherlockDialogFragment
implements OnClickListener {
private ListView listView;
private ArrayAdapter<String> adapter;
private String[] myItemArray;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
myItemArray = getResources().getStringArray(R.array.my_item_array);
listView = (ListView) getActivity().findViewById(R.id.dialog_listView);
adapter = new ArrayAdapter<String>(getActivity(), R.id.row_relativeLayout, myItemArray);
listView.setAdapter(adapter);
return new AlertDialog.Builder(getActivity())
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, null))
//.setAdapter(adapter, this)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
并且有效。
这是自定义行条目的XML,我在教程中找到了(找不到链接了):
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice, myItemArray);
我不知道会出现什么问题......
答案 0 :(得分:2)
你试图在给布局充气之前找到你的列表。
在方法的开头试试这个:
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
//if you only have one listview in your layout, v is your listview
// ListView lv = (ListView)v;
ListView lv = v.findById(R.id.dialog_listView);
然后,在返回语句中给予v
代替或夸大布局。
return new AlertDialog.Builder(getActivity())
.setView(v)...
答案 1 :(得分:0)
你应该在onCreateView方法
中使用你自己的dialogFragment布局例如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout to use as dialog or embedded fragment
View v = inflater.inflate(R.layout.layout_frogos_password, container, false);
Button buttonOk = (Button) v.findViewById(R.id.buttonForgotPasswordDialogOk);
buttonOk.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mListener.onDialogPositiveClick(ForgotPasswordDialog.this);
}
});
return v;
}
/** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// The only reason you might override this method when using
// onCreateView() is
// to modify any dialog characteristics. For example, the dialog
// includes a
// title by default, but your custom layout might not need it. So here
// you can
// remove the dialog title, but you must call the superclass to get the
// Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}