【问题标题】:DialogFragment with custom ListView带有自定义 ListView 的 DialogFragment
【发布时间】:2013-12-03 16:08:42
【问题描述】:

我正在尝试创建一个 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);

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

【问题讨论】:

  • 对我来说,将 listView 的高度从 0dp 设置为 200dp 一样简单。不需要 AlertDialog.Builder 或覆盖 onCreateDialog。

标签: android android-listview dialogfragment


【解决方案1】:

您应该覆盖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, 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();
}

DialogFragmentdocumentation page 中的这句话描述了您正在尝试做的事情:

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

在您的情况下,似乎onCreateDialog 是要走的路,因为您想做一个自定义内部视图。

【讨论】:

  • 我认为您使用 onCreateDialog 是正确的。但是您提供的代码无法工作,因为 onCreateDialog 想要一个 Dialog 来返回而不是 View...
  • @TrtG 哎呀,我把添加完整代码的编辑搞砸了做 setTitle(int) 因为这将被解析为字符串资源,而不是将 int 实际转换为文本,因此您应该像我一样明确地将其设为字符串。
  • 只有一件事。您对 builder.setTitle() 所做的更改不起作用,它将 int itfself 打印为字符串。所以你得到一个像“256542”这样的标题。我把空字符串连接去掉了就ok了。
【解决方案2】:

可能是您遗漏了一些很小但很重要的东西。您的适配器中是否缺少notifyDataSetChanged()

“将新项目添加到适配器后,您必须调用 notifyDataSetChanged() 以便 listview 使用适配器中找到的新数据集刷新自身。”

【讨论】:

  • 我已经添加了它,但它并没有改变任何东西。我认为这不是问题,因为我在常规片段中使用相同的适配器并且效果很好。
【解决方案3】:

我忘记的是:

view.setAdapter(adapter);

在我添加代码有效之后

【讨论】:

  • 问题中的代码中没有任何名为viewadapter 的变量,因此不清楚您将这段代码放在哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多