DialogFragment with custom ListView
Asked Answered
B

3

6

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?

Bobbitt answered 3/12, 2013 at 16:8 Comment(1)
For me it was as easy as setting my listView's height from 0dp to 200dp. Didn't need an AlertDialog.Builder or override onCreateDialog.Heresy
M
12

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.

Monotonous answered 3/12, 2013 at 16:19 Comment(3)
I think you are right about using onCreateDialog. But the code you supplied can't work since onCreateDialog wants a Dialog for return not a View...Bobbitt
@Bobbitt Oops yep, I messed up my edit of adding the full code :) Couple small notes about the builder: 1. you can change it (setTitle() returns the builder) 2. It's bad to do setTitle(int) because that will get parsed as a string resource, not an actual cast of the int as text, so you should explicitly make it a string like I did.Monotonous
Just one thing. The change you made on builder.setTitle() is not working, it's printing the int itfself as a string. So you get a title like "256542". I took off the empty string concatenation and it is ok.Bobbitt
L
0

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."

Liquor answered 3/12, 2013 at 16:29 Comment(1)
I've added it but it didn't change anything. I don't think this can be the problem because I use the same adapter in regular fragments and it works well.Bobbitt
C
0

what I forgot was:

view.setAdapter(adapter);

after I added that the code worked

Condyle answered 10/11, 2016 at 22:10 Comment(1)
There isn't any variable called view or adapter in the code in the question, so it isn't clear where you are putting this code.Gondolier

© 2022 - 2024 — McMap. All rights reserved.