我有一个名为strength
的微调器,我需要获取它的值并将其保存到SharedPreferences。然后,当用户再次启动相同的活动(SaAStrength
)时,需要将名为strength
的微调器设置为他们已选择的活动。
我目前的代码没有将微调器设置为Prefs中保存的字符串。
我很失落如何处理这个问题。
package com.mikitz.rogsimple;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SaAStrength extends Activity {
Spinner strength, melee, h2h, parry;
SharedPreferences pref;
String getstrength, getmelee, geth2h, getparry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saa_strength);
pref = getSharedPreferences("Character1", Context.MODE_PRIVATE);
strength = (Spinner) findViewById(R.id.strength);
strength = (Spinner) findViewById(R.id.strength);
Integer[] items1 = new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
ArrayAdapter<Integer> adapter1 = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items1);
strength.setAdapter(adapter1);
melee = (Spinner) findViewById(R.id.melee);
Integer[] items2 = new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
ArrayAdapter<Integer> adapter2 = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items2);
melee.setAdapter(adapter2);
h2h = (Spinner) findViewById(R.id.h2h);
Integer[] items3 = new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
ArrayAdapter<Integer> adapter3 = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items3);
h2h.setAdapter(adapter3);
parry = (Spinner) findViewById(R.id.parry);
Integer[] items4 = new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
ArrayAdapter<Integer> adapter4 = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items4);
parry.setAdapter(adapter4);
getstrength = pref.getString("strength", "");
strength.setSelection(((ArrayAdapter)strength.getAdapter()).getPosition(getstrength));
}
public void onBackPress()
{
SharedPreferences.Editor editor=pref.edit();
editor.putString("strength", strength.getSelectedItem().toString());
editor.commit();
Toast.makeText(this, "GREAT SUCCESS!!!!", Toast.LENGTH_LONG).show();
Intent intent = new Intent (this, NewSkillsAndAttributes.class);
startActivity(intent);
}
}