(Android) List in a dialog box with a single item with a checkbox

Ok, I have been looking for this for a while, but can’t find anything. My application displays the school curriculum of students in the list, and when a user clicks on a class in the list, it displays a dialog box with several options ("Change", "Delete", "Set alarm"). Editing and deleting is easy because they are clicked and something happens, but I need help with the "Set alarm" option. I don’t want it to be accessible for clickability, I just want to have a flag to the right of it that turns the alarm on or off. Here is the code for my dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(courseName) .setItems(R.array.courseList, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch(which) { case LIST_EDIT: break; case LIST_DELETE: break; case LIST_ALARM: break; } } }); AlertDialog alert = builder.create(); alert.show(); 

Now I have the list options in an array of strings in my XML resource file with id courseList. LIST_EDIT, LIST_DELETE and LIST_ALARM are final ints corresponding to their index in the list. I'm really not sure how to add a checkbox to the alarm list item, any help would be appreciated.

+4
source share
1 answer

If you are familiar with the fragment , you can use FragmentDialog , and then in the onCreateDialog method you create a dialog as you wish.

 LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.list, null); _list = (ListView) view.findViewById(R.id.listview); _adapter = new AdapterDialog(_context); _list.setAdapter(_adapter); // AlertDialog.Builder builder = new AlertDialog.Builder(_context); builder.setView(view); builder.setTitle("Dialog").setPositiveButton(getActivity().getString(android.R.string.ok), this); return builder.create(); 

Or something like that Then create a dialog using the singleton method or constructor

 _dialog = Dialog.newInstance(R.string.title, this); _dialog.setCancelable(true); _dialog.show(getSupportFragmentManager(), null); 

With your aproach you cannot add anything more than “buttons”, if you want to have a custom line in your list, you should use an adapter

hope this helps

+1
source

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


All Articles