views:

178

answers:

1

Hello!

I want to save text from 2 edittexts(et1,et2) and selection from 3 spinners(spinnerm,spinner and spinner2) onPause and setting them back onResume. Text from edittexts is saved correctly, only spinner selection don't work as desired. My code:

  public void onResume(){
     super.onResume();
     Log.d("REZUM","notr smo");
     SharedPreferences seti = getSharedPreferences( "pavzica", MODE_PRIVATE);

     spinnerm.setSelection(seti.getInt("m", 0));
         spinner.setSelection(seti.getInt("k", 0));
         spinner2.setSelection(seti.getInt("p", 0));
         et1.setText(seti.getString("zade", ""));
         et2.setText(seti.getString("Vseb", ""));




 }

public void onPause() {
    shraniPolja();
    super.onPause();
}


public void shraniPolja() {
    SharedPreferences seti = getSharedPreferences( "pavzica", MODE_PRIVATE);
    SharedPreferences.Editor edito = seti.edit();

    edito.putString("zade", et1.getText().toString());
    edito.putString("Vseb", et2.getText().toString());
    edito.putInt("m", spinnerm.getSelectedItemPosition());
    edito.putInt("k", spinner.getSelectedItemPosition());
    edito.putInt("p", spinner2.getSelectedItemPosition());
    edito.putBoolean("b", true);
    edito.commit();

}

What am i doing wrong?

A: 

you need to invalidate the view after calling setSelection / setText.

you can invalidate the entire view with mYourView.invalidate(); or you can invalidate just part of a view or a particular drawable. See the View class documentation.

If you want the spinner animation to run as the spinner gets to the value on resume, use the form of setSelection that has a second boolean parameter and pass true in.

Mark
I have setOnItemSelectedListener registered in onCreate on first spinner. In onItemSelected i set the adapters fro 2. and 3. spinner. I get null pointer exception if i try spinner2.getAdapter().toString(). I tried with registering listener again in onResume but that doesnt help either.I tried with: spinner.setSelection(seti.getInt("k", 0)); spinner.invalidate(); spinner2.setSelection(seti.getInt("p", 0)); spinner2.invalidate();But that doesnt help :(
DixieFlatline
Only the selection for the first spinner is remebered correctly. It has to do something with onItemSelected being executed after onResume and then resetting spinners to their original positions.
DixieFlatline
How to call onitemSelected() immediatly in onResume() to set the adapters for 2. and 3. spinner correctly and then setting selection for 2. and 3. spinner?
DixieFlatline
I solved the issue by changing onItemSelected method to verify if i was in onPause before (with boolean). Thank you all anyway.
DixieFlatline