如何使用共享首选项在微调器中保存和加载字符串值
1.不能存储值
2.无法加载值
阵列
<string-array name="country_array">
<item>Greece</item>
<item>United Kingdom</item>
<item>Italy</item>
<item>France</item>
<item>Germany</item>
<item>Turkey</item>
<item>Poland</item>
<item>India</item>
</string-array>
Main.java
public class SaveSpinShared extends Activity implements OnItemSelectedListener {
ArrayAdapter adapter;
String getList, loadedList;
String save;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save_spin);
SavePreferences("LIST", "France");
// load
LoadPreferences();
getList = loadedList;
Spinner sp = (Spinner) findViewById(R.id.SaveSpin);
adapter = ArrayAdapter.createFromResource(this, R.array.country_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
//save
Button buttonPlay = (Button) findViewById(R.id.btnSavespin);
buttonPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SavePreferences("LIST", save);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// TODO Auto-generated method stub
save = parentView.getItemAtPosition(position).toString();
Toast.makeText(parentView.getContext(), "Result : " + save,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
/* Save password */
public void SavePreferences(String key, String value) {
SharedPreferences data = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = data.edit();
editor.putString(key, value);
editor.commit();
}
/* Load password */
public void LoadPreferences() {
SharedPreferences data = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
loadedList = data.getString("LIST", "empty");
}
}
答案 0 :(得分:0)
您不需要共享首选项来在微调器中加载值。您只需要在string.xml文件中声明数组并加载它。我给你我的代码。只需使用它。:
<string-array name="country_array">
<item>Greece</item>
<item>United Kingdom</item>
<item>Italy</item>
<item>France</item>
<item>Germany</item>
<item>Turkey</item>
<item>Poland</item>
<item>India</item>
</string-array>
<Spinner
android:id="@+id/spinCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="8dp"
android:popupBackground="@android:color/white"
android:scrollbars="none"
android:spinnerMode="dropdown" />
Spinner spinCountry;
spinCountry= (Spinner) findViewById(R.id.spinCountry);//fetch the spinner from layout file
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.country_array));//setting the country_array to spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCountry.setAdapter(adapter);
//if you want to set any action you can do in this listener
spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
SavePreferences("LIST",spinCountry.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
答案 1 :(得分:-1)
你不能使用sharepreferences。它存储的数据非常小,几乎原始数据不是数组值(对象)。我希望你能理解。