DialogFragment with custom ListView

六眼飞鱼酱① 提交于 2019-12-30 04:45:14

问题


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


}

When calling it from a fragment :

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?


回答1:


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

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

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.

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




回答2:


May be you are missing something very small but important. Are you missing notifyDataSetChanged() in your adapter?

"Once you have added the new item to the adapter you have to call notifyDataSetChanged() so that the listview refreshes itself with the new set of data found in the adapter."




回答3:


what I forgot was:

view.setAdapter(adapter);

after I added that the code worked



来源:https://stackoverflow.com/questions/20356038/dialogfragment-with-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!