具有自定义ListView的DialogFragment [英] DialogFragment with custom ListView

查看:144
本文介绍了具有自定义ListView的DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个DialogFragment,该对话框片段显示一个内部带有自定义ListView的对话框.

I'm trying to create a DialogFragment that shows a dialog with a custom ListView inside.

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);

它仅显示一个带有标题的空白对话框...为什么我的列表没有显示?

It only shows a blank dialog with the title... why my isn't my list displayed?

推荐答案

而不是使用onCreateView,您应该覆盖onCreateDialog,并且在其内部,其外观类似于:

Instead of using onCreateView you should be overriding onCreateDialog and inside of it, it'll look something like:

@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 文档页面中的这句话引述了您的内容正在尝试:

This quote from the DialogFragment documentation page describes what you're trying to do:

实现应重写此类,并实现onCreateView(LayoutInflater, ViewGroup, Bundle)以提供对话框的内容.或者,他们可以覆盖onCreateDialog(Bundle)来创建一个完全自定义的对话框,例如AlertDialog,并带有其自己的内容.

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,因为您要执行自定义内部视图.

In your case, it seems like onCreateDialog is the way to go since you want to do a custom inner view.

这篇关于具有自定义ListView的DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆