DialogFragment with custom ListView

前端 未结 3 1874
小蘑菇
小蘑菇 2021-01-03 00:47

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

public class MultiSelectDialogCustom extends DialogFragment {


            


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 01:09

    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.

提交回复
热议问题