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

查看:25
本文介绍了带有自定义 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天全站免登陆