我在設置警報對話框中的默認選定項目時遇到了一些問題。以下是我在我的代碼中使用:android獲取警報對話框默認選定項目
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();
所以我現在的問題是我設置所選項目根據一些計算,如果我不選擇任何內容,然後按確定,不,我已經被選定的項目事默認它是再次創建對話框,因爲如果用戶沒有選擇任何東西,這就是想法。我試圖設置item=selected;
或selected=item;
,但它不起作用。我知道我的問題是合乎邏輯的,但我無法弄清楚。任何建議如何讓事情發揮作用?
你能具體談談你的問題。 –
這個想法是讓用戶選擇他想要存儲我的應用程序文件的存儲。默認情況下,我設置選擇的存儲空間更多,但問題是如果我不選擇任何項目,並使用默認選定項目單擊確定,它不會設置任何內容。這就是爲什麼我想找到一種方法來使用默認項目,如果用戶不選擇任何東西來創建正確的存儲路徑。 –