-2

Hi i wanted to create a view exactly like thisImage.

Once the item in a list view is clicked, a spinner with radio buttons should open.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
mithu
  • 177
  • 1
  • 6
  • 16
  • this is a dialog with listview. Check this answer http://stackoverflow.com/a/6157258/1109425 – nandeesh Oct 15 '12 at 05:44
  • You need to create custom Spinner. Check this [Link](http://app-solut.com/blog/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/). it help you to create custom spinner l – Md Abdul Gafur Oct 15 '12 at 05:43

2 Answers2

3

If you want to display a spinner for every list item clicked in ListView. Its possible with AlertDialog.

Try to create the alert dialog with radio buttons by using this

and try this block

 list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0,
        View arg1, int position, long arg3) 
        {
           AlertDialogView();
        }
     }

And the code for AlertDialogView() will be like this

private void AlertDialogView()
{
        final CharSequence[] items = {"15 secs", "30 secs", "1 min", "2 mins"};

        AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
        builder.setTitle("Alert Dialog with ListView and Radio button");
        builder.setIcon(R.drawable.icon);
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
           }
      });

       builder.setPositiveButton("Yes",
 new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
           Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
       }
       });
       builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
         Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
       }
      });
      AlertDialog alert = builder.create();
      alert.show();
      }
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • I followed the same code to display the dialog in listview with radio buttons. When I click on a Listview item, the dialog opens and I click on the items in the dialog, and close the dialog after clicking "Yes" button. When the dialog is opened once again the selected item is not shown. How to show the selected item? – mithu Oct 15 '12 at 06:32
  • well, you should save your results either in `localdatabase` or better use preference manager to save data and every time just maintain the If condition, if the item is in preferences.xml so that set the position of the selected item in alert dialog,other wise display normal alert dialog – Ram kiran Pachigolla Oct 15 '12 at 06:36
  • Ok I will store the value using the Shared Preferences once it is clicked. How to retrieve it and display it as a selected item in the dialog? Please help – mithu Oct 15 '12 at 06:54
  • It will get through the preferences, but it will be complicated. just use localdatabase(Sqlite) for this – Ram kiran Pachigolla Oct 15 '12 at 07:15
  • If you want i will post how to get your result – Ram kiran Pachigolla Oct 15 '12 at 07:16
  • 1
    I stored the value after each click using Shared Preferences. I retrieved the value from Shared Prefrerence and checked it's value. If it is not null, I call the dialog with the position of the item. Else I call the dialog with position == -1. I got it working. Thanks for your help. – mithu Oct 15 '12 at 07:49
  • @Ramkiran.i have so problem and i use your code,thanks! but i have question ... please help me-i want to have selected-item of spinner in my list view(e.g. "1 min" is selected and it shows in my list view ,although it saves in my SqlLite dataBase).how can i do thid? – Mina Dahesh Jul 18 '15 at 07:13
0

in list view whenever you call any item just get the position of clicked item,say 9th element of list view is click on that time call some method,after calling method if your spinner adapter is depends on list of clicked item than first as per the list view's clicked item position fill the spinner adapter than simply open spinner and after you close your spinner and again open with different item of list view than change the spinner adapter.this simple logic should solve your issue i guess thanks Aamirkhan i,

Aamirkhan
  • 5,746
  • 10
  • 47
  • 74