DialogFragment with custom ListView

I am trying to create a DialogFragment dialog 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 an empty dialog with a title ... why is my list not showing?

+6
source share
3 answers

Instead of using onCreateView you should override onCreateDialog and inside it, it will look something like this:

 @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 on the DialogFragment documentation DialogFragment describes what you are trying to do:

Implementations must override this class and implement onCreateView(LayoutInflater, ViewGroup, Bundle) to provide the contents of the dialog box. Alternatively, they can override onCreateDialog(Bundle) to create a fully customizable dialog, such as AlertDialog , with its own content.

In your case, it seems that onCreateDialog is the way to go since you want to make your own internal look.

+11
source

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

"After you add a new item to the adapter, you must call notifyDataSetChanged() so that the listview updated with the new dataset found in the adapter."

0
source

I forgot:

 view.setAdapter(adapter); 

after i added that the code worked

0
source

Source: https://habr.com/ru/post/959252/


All Articles