2013-08-06 51 views
-2

我創建了一個帶有單選按鈕的警報對話框,當我選擇單選按鈕軌道列表時,它在下次選擇時不會記住軌道,所以我無法知道哪個軌道是目前在玩。 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; 
} 

} 
你需要或者其存儲到數據庫,存儲或使用靜態變量和更改變量隨時使用者的價值
+3

你是什麼意思的「不記得」? –

+0

@Dany Joumaa它不顯示列表中先前選擇的項目。 –

回答

0
Radio button does not preserve value. So you loose them after exiting. You can handle radio button scenario in combination with sharedpreferences. 

After setting radio button add it to shared preferences like: 

private RadioButton mWifiDataButton = null; 
private RadioButton mWifiButton = null; 

in onCreate() .. 

mWifiDataButton = (RadioButton) findViewById(R.id.radio_wifi_data); 
mWifiDataButton.setOnClickListener(this); 

mWifiButton = (RadioButton) findViewById(R.id.radio_wifi_only); 
mWifiButton.setOnClickListener(this); 



Editor ed = pref.edit(); 
ed.putBoolean("pref_radio_wifidata",mWifiDataButton.isChecked()); 
ed.putBoolean("pref_radio_wifi",mWifiButton.isChecked()); 

and next time retrieve the values like this: 

mWifiDataButton.setChecked(pref.getBoolean("pref_radio_wifidata", true)); 
mWifiButton.setChecked(pref.getBoolean("pref_radio_wifi", false)); 



Then in onClick() handle it properly: 

@Override 
    public void onClick(View v) { 
     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     switch(v.getId()){ 


      case R.id.radio_wifi_data: 
       pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
       boolean wifidataState = pref.getBoolean("pref_radio_wifidata", true); 
       mWifiDataButton.setChecked(!wifidataState); 
       mWifiButton.setChecked(wifidataState); 
       Editor ed = pref.edit(); 
       ed.putBoolean("pref_radio_wifidata", !wifidataState); 
       ed.putBoolean("pref_radio_wifi", wifidataState); 
       ed.commit(); 
       break; 
      case R.id.radio_wifi_only: 
       pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
       boolean wifiState = pref.getBoolean("pref_radio_wifi", false); 
       mWifiButton.setChecked(!wifiState); 
       mWifiDataButton.setChecked(wifiState); 
       Editor ed1 = pref.edit(); 
       ed1.putBoolean("pref_radio_wifi", !wifiState); 
       ed1.putBoolean("pref_radio_wifidata", wifiState); 
       ed1.commit(); 
       break; 

ed.commit(); 
+0

嗨賽..如果任何答案解決您的目的,請接受答案,以便其他人可以通過輕鬆獲得答案來獲益。謝謝 – Sushil

1

改變radiobox。

乾杯,

+0

你可以解釋一下靜態變量嗎? –

+0

對於只存儲小值,數據庫不應該按照android指南使用。共享偏好是最好也是最簡單的方法.. – Sushil

+0

@SaiKiran你可以按照Sushil方法,實際上,它會工作得很好,看起來相當一致的Android開發。正如我所說的,關於靜態val,你可以聲明它爲一個分離的類。我不記得確切,但它是像公共靜態字符串MY_VAL =「」; –