我正在尝试创建一个DialogFragment,它显示一个内部有自定义ListView的对话框。
public class MultiSelectDialogCustom extends DialogFragment {
ListView mLocationList;
private ArrayList<String> mOfficeListItems = new ArrayList<String>();
public static MultiSelectDialogCustom newInstance(int title) {
MultiSelectDialogCustom dialog = new MultiSelectDialogCustom();
Bundle args = new Bundle();
args.putInt("title", title);
dialog.setArguments(args);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Collections.addAll(mOfficeListItems, getResources().getStringArray(R.array.offices));
View v = inflater.inflate(R.layout.fragment_choice_list, container,
true);
mLocationList = (ListView)v.findViewById(R.id.location_criteria_list);
final FunctionListArrayAdapter adapter = new FunctionListArrayAdapter(
this, android.R.layout.simple_list_item_1, mOfficeListItems);
mLocationList.setAdapter(adapter);
getDialog().setTitle(getArguments().getInt("title"));
return v;
}
}
从片段中调用它时:
MultiSelectDialogCustom dialogFrag = MultiSelectDialogCustom_.newInstance(R.string.dialog_title);
dialogFrag.show(getActivity().getSupportFragmentManager(), null);
它只显示一个带有标题的空白对话框...为什么我的列表不显示?
答案 0 :(得分:11)
而不是使用onCreateView
,你应该覆盖onCreateDialog
,而在其中,它看起来像是:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Collections.addAll(mOfficeListItems, getResources().getStringArray(R.array.offices));
View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_choice_list, null);
mLocationList = (ListView)v.findViewById(R.id.location_criteria_list);
final FunctionListArrayAdapter adapter = new FunctionListArrayAdapter(
this, android.R.layout.simple_list_item_1, mOfficeListItems);
mLocationList.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getInt("title")).setView(v);
return builder.create();
}
来自DialogFragment
documentation page的引用描述了您要做的事情:
实现应覆盖此类并实现
onCreateView(LayoutInflater, ViewGroup, Bundle)
以提供对话框的内容。或者,他们可以覆盖onCreateDialog(Bundle)
以创建完全自定义的对话框,例如AlertDialog
,并拥有自己的内容。
在您的情况下,似乎onCreateDialog
是您要进行自定义内部视图的方法。
答案 1 :(得分:0)
可能你错过了一些非常小而重要的东西。您是否遗漏了适配器中的notifyDataSetChanged()
?
“将新项目添加到适配器后,您必须调用notifyDataSetChanged()
,以便listview
使用适配器中找到的新数据集刷新自身。”
答案 2 :(得分:0)
view.setAdapter(adapter);
我添加代码后