android – 带自定义ListView的DialogFragment

前端之家收集整理的这篇文章主要介绍了android – 带自定义ListView的DialogFragment前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个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);

它只显示一个带有标题的空白对话框…为什么我的列表不显示

解决方法

你应该重写onCreateDialog而不是使用onCreateView,它应该看起来像:
@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,mOfficeListItems);
    mLocationList.setAdapter(adapter);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(getArguments().getInt("title")).setView(v);

    return builder.create();
}

DialogFragment documentation page的引用描述了您要做的事情:

Implementations should override this class and implement onCreateView(LayoutInflater,ViewGroup,Bundle) to supply the content of the dialog. Alternatively,they can override onCreateDialog(Bundle) to create an entirely custom dialog,such as an AlertDialog,with its own content.

在你的情况下,似乎onCreateDialog是你想要做自定义内部视图的方法.

原文链接:https://www.f2er.com/android/309066.html

猜你在找的Android相关文章