我有一个基本的警报对话框,我在第一次加载我的应用程序时运行。当在我的“onOptionsItemSelected”中选择一个选项时会弹出警报它加载时间列表并默认选择“4”“30秒”。
但是假设用户只是打开它并且不更改任何设置并单击确定,“哪个”应该使我的“notificationChoice”为4,但它使其变为0。直到我实际选择一个位置“哪个”将返回0。当我选择一个位置按“确定”,重新打开通知,然后单击“确定”我会从“哪个”得到正确的结果。这是故意的还是我做错了什么?
private void changeNotificationTimer() {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
String[] timeModes = { "10 Seconds.", "15 Seconds.", "20 Seconds.",
"25 Seconds.", "30 Seconds.", "45 Seconds.", "1 Minute." };
final int timerPos = settings.getInt("timerPos", 4);
System.out.println("timerPos : " + timerPos);
// Where we track the selected item
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle("Set Notification Timer");
// Lets set up the single choice for the game mode.
builder.setSingleChoiceItems(timeModes, timerPos,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
notificationChoice = which;
System.out.println("which : " + which);
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println("notificationChoice : " + notificationChoice);
// Set the shared prefs.
if (timerPos != notificationChoice) {
System.out.println(timerPos + " != " + notificationChoice);
setSharedNotificationPrefs(notificationChoice);
}
}
}).create().show();
}