我正在尝试创建一个DialogFragment,它显示一个带有自定义ListView的对话框.
public class MultiSelectDialogCustom extends DialogFragment { ListView mLocationList; private ArrayListmOfficeListItems = new ArrayList (); 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);
它只显示一个带有标题的空白对话框...为什么我的列表不显示?
而不是使用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
文档页面中的引用描述了您要执行的操作:
实现应该覆盖此类并实现
onCreateView(LayoutInflater, ViewGroup, Bundle)
以提供对话框的内容.或者,他们可以覆盖onCreateDialog(Bundle)
以创建完全自定义的对话框,例如AlertDialog
,具有自己的内容.
在您的情况下,似乎onCreateDialog
是要走的路,因为您想要自定义内部视图.