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?