我使用OnItemSelectedListener函数创建了一个微调器。我是从一个数组中创建的,其中包含4个选择:
String[] arrayOfString = new String[4];
arrayOfString[0] = "Kiskunfélegyháza HQ [160kbps MP3]";
arrayOfString[1] = "Kiskunfélegyháza MQ [64kbps AAC]";
arrayOfString[2] = "Kiskunmajsa HQ [160kbps MP3]";
arrayOfString[3] = "Kiskunmajsa MQ [64kbps AAC]";
this.items = arrayOfString;
正在加载数据:
PreferenceManager.setDefaultValues(this, 0x7f040000, false);
final SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
this.mWebView_enabled = localSharedPreferences.getBoolean("mWebView_enabled", true);
this.savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port");
this.savszelesseg_saved = this.savszelesseg_port;
final Spinner localSpinner = (Spinner)findViewById(R.id.savszelesseg);
ArrayAdapter localArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, this.items);
localArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localSpinner.setAdapter(localArrayAdapter);
如果有人选择了某个选项,它将保存到SharedPreferencies并更改音乐。但是,当有人退出然后重新打开应用程序时,如何取回数据?
我把它保存为savszelesseg_mentes,然后保存savszelesseg_port。我还有一个选项菜单来保存此设置。
localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View paramView, int paramInt, long paramLong)
{
if (!savszelesseg_port_modositva)
{
savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port");
savszelesseg_port_modositva = true;
}
savszelesseg_beallitas = items[paramInt];
if (savszelesseg_beallitas == "Kiskunfélegyháza HQ [160kbps MP3]")
{
savszelesseg_port = "8500/relay_911";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (savszelesseg_beallitas == "Kiskunfélegyháza MQ [64kbps AAC]")
{
savszelesseg_port = "8500/relay_mobile_911";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (savszelesseg_beallitas == "Kiskunmajsa HQ [160kbps MP3]")
{
savszelesseg_port = "8500/relay_882";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (savszelesseg_beallitas == "Kiskunmajsa MQ [64kbps AAC]")
{
savszelesseg_port = "8500/relay_mobile_882";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (lejatszas == 1 && savszelesseg_port_modositva)
{
stopPlaying();
startPlaying();
}
if (lejatszas == 0 && savszelesseg_port_modositva == true)
{
startPlaying();
}
}
如果没有选择:
public void onNothingSelected(AdapterView parentView)
{
}
答案 0 :(得分:2)
你正在比较字符串而不是引用,所以使用equals()方法不要使用==
您正在使用
if (savszelesseg_beallitas == "Kiskunfélegyháza HQ [160kbps MP3]")
改为使用
if (savszelesseg_beallitas.equals("Kiskunfélegyháza HQ [160kbps MP3]"))
或
if(“KiskunfélegyházaHQ[160kbps MP3]”。equals(savszelesseg_beallitas));
答案 1 :(得分:0)
这样的事情,你应该做的是获取数据,然后设置正确的选择:
int index = 0;
String savedString = localSharedPreferences.getString("savszelesseg_mentes", "");
for(int i = 0; i < arrayOfString.length(); i++){
if(savedString.equals(arrayOfString[i])){
index = i;
break;
}
}
localSpinner.setSelection(index, true);