0

I have a Spinner named strength that I need to grab its value and save it to SharedPreferences. Then when the user starts the same activity (SaAStrength) again, the Spinner named strength needs to be set to what they already selected.

The code I have currently doesn't set the spinner to the saved string in Prefs.

I'm quite lost as to how to approach this.

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);
}
}
Mikitz06
  • 340
  • 3
  • 14

0 Answers0