我創建了一個帶有單選按鈕的警報對話框,當我選擇單選按鈕軌道列表時,它在下次選擇時不會記住軌道,所以我無法知道哪個軌道是目前在玩。 int currentselection = -1;
請檢查代碼並告訴我,如果我的代碼有任何問題。在此先感謝..單選按鈕的選擇不存儲Android
public class MainActivity extends Activity {
Button b1;
int currentselection = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
final CharSequence[] items = { "Track #1", "Track #2","Track #3" };
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Track");
builder.setIcon(R.drawable.ic_launcher);
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setSingleChoiceItems(items, currentselection,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface mdialog, int item) {
// TODO Auto-generated method stub
switch (item) {
case 0:
currentselection = 0;
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
case 1:
currentselection = 1;
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
case 2:
currentselection = 2;
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
}
mdialog.cancel();
}
});
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = builder.create();
alert.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
你需要或者其存儲到數據庫,存儲或使用靜態變量和更改變量隨時使用者的價值
你是什麼意思的「不記得」? –
@Dany Joumaa它不顯示列表中先前選擇的項目。 –