Android set the selected item in the warning dialog

I am creating an alert dialog at the beginning of my application to allow the user to choose where to store the data that my application downloads from the Internet. What I want to achieve now depends on the size of the internal / external storage. I want to install the selected one of the elements. Here is the code I use to create the dialog:

@SuppressWarnings("static-access")
public void createDialog(){


    final CharSequence[] items = {"Phone Memory - "+memorysize+" free space", "SD Card - "+megAvailable+" MB free space"};

    final int userId = rpc.getUserId(this);
    final String servername = rpc.getCurrentServerName(this);

    SharedPreferences stampiiSettings = PreferenceManager.getDefaultSharedPreferences(MyCollectionList.this);
    final SharedPreferences.Editor editor = stampiiSettings.edit();

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
    builder.setTitle("Select Storage Path");
    builder.setSingleChoiceItems(items, -1, 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.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi); // update UI            
        }
        });
        AlertDialog alert = builder.show();
}

And one more thing I want to achieve, how can I prevent the user from closing the notification dialog box if he does not select any item. I do not want to close the dialog by clicking the "Back" button or when he clicks the "OK" button. Any ideas / suggestions / help are appreciated!

+5
2

- :

int selected = 0; // or whatever you want
builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
              //onclick
    }});
+17

,

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // no need to write anything here just implement this interface into this button
        }
});

spinner , , .

int selected = 0; // if you want in integer or 
String selected = "internal"; // you can go with string

ok

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
            // here get the selected item value 
    }
});
+3

All Articles