1

I have a little problem with setting the default selected item in Alert Dialog. Here is what I use in my code :

if(memory>megAvailable){
        selected = 0;
    } else if(megAvailable>memory){
        selected = 1;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
    builder.setTitle("Select Storage Path");
    builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if(item == 0){
                rpc.createFoldersInInternalStorage(servername, userId, MyCollectionList.this);
                Toast.makeText(getApplicationContext(), "Selected Storage Path : Phone Memory", Toast.LENGTH_SHORT).show();
                editor.putInt("storagePath", 1);
                editor.commit();
            } else if (item == 1){
                rpc.createFoldersInExternalStorage(servername, userId, MyCollectionList.this);
                Toast.makeText(getApplicationContext(), "Selected Storage Path : SD Card", Toast.LENGTH_SHORT).show();
                editor.putInt("storagePath", 2);
                editor.commit();
            }
        }});

        builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi);     
        }
        });
        AlertDialog alert = builder.create();

So my problem now is I set the selected item depending on some calculations and if i don't select anything and press OK, no matter that I have selected item by default it's creating the dialog again, because that's the idea if user didn't select anything. I've tried to set item=selected; or selected=item; but it's not working. I know my problem is logical , but I can't figure it out. Any suggestions how to get the things to work?

Android-Droid
  • 14,365
  • 41
  • 114
  • 185
  • could you be more specific about your problem. – Lalit Poptani Nov 18 '11 at 15:10
  • The idea is to let the user select the storage where he wants to store the files of my application. By default I'm setting selected the storage which have more space, but the problem is if i don't choose any of items and click OK with the default selected item, it's not setting anything. That's why I want to find a way to use default item if user not choose anything to create the right storage paths. – Android-Droid Nov 18 '11 at 15:15

1 Answers1

1

You can just put the code that sets your storagePath in the onClickHandler attached to the NegativeButton:

final int defaultSelected = selected +1; //this is final since you need to access it in the anonymous inner class; we're adding 1 since your value that you write seems to be either 1 or 2.
 builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi);     
                editor.putInt("storagePath", defaultSelected);
                editor.commit();
        }
        });
Chris
  • 22,923
  • 4
  • 56
  • 50
  • yeah but if the user select an item I won't get his choice – Android-Droid Nov 18 '11 at 15:40
  • Why don't you just set the value to the default before you display the dialog box, then? You could be doing an unnecessary write, but writing to a preference object isn't that expensive. – Chris Nov 18 '11 at 15:46
  • Actually I saw where was my problem, it seems that I was clearing all my shared preferences in app start that's why the dialog opens everytime ,but your idea is still correct so I will accept your answer. Thanks for help! – Android-Droid Nov 18 '11 at 15:56