SharedPreferences Save value of Int in a TextView

2020-08-09 04:23发布

问题:

I try to save the value of an int "redRing" from the class "Ring" displayed in a TextView of another activity using SharedPreferences, it can work but not everytime, sometimes there's a reset of the value of my int if I quit the app and come back again in my app but not everytime. I did a simple small example of code if someone can try to say me what's wrong with my code?

1) The class Ring :

public class Ring { // User search rings with different colors

public static int color;
public static int redRing, greenRing, nbRing, somment, someGreen, someRing;
public static TextView tvNbRing, tvRingColor;
// Constuctor
public Ring(int c) { this.color = c; }
// Getter
public int getColor() { return this.color; }

public void setColor(int dice) { // For the example there’s 2 colors
    dice = (int)(Math.random()*2+1);
    this.color = dice;
    if(this.color == 1) { // If 1 so user find a red ring
        redRing = someint + 1;
        tvRingColor.setText("It's a red ring");
    }
    if(this.color == 2) {
        greenRing = someGreen + 1; // I add the green color
        tvRingColor.setText("It's a green ring");
    }
}

public int setNbRingFind() { // Nb = Number
    int rings = (int)(Math.random()*2+0); // User can find 0 or 1 ring
    Ring r = new Ring(this.color);
    if(rings>0) {
        nbRing = someRing + 1;
        tvNbRing.setText("You find " + rings + " ring");
        r.setColor(this.color);
    } else { tvNbRing.setText("Nothing find, try again"); }
        return rings;
}
}

2) Activity1 : Main

import static fr.ringshared.Ring.tvNbRing;
import static fr.ringshared.Ring.tvRingColor;
import static fr.ringshared.Ring.redRing;
import static fr.ringshared.Ring.someint;
import static fr.save.sharedpreferences.Ring.greenRing;
import static fr.save.sharedpreferences.Ring.nbRing;
import static fr.save.sharedpreferences.Ring.someGreen;
import static fr.save.sharedpreferences.Ring.someRing;

public class MainActivity extends Activity {

Button search, stock, useRing;
TextView tvRing;
int x = 0;
Ring r = new Ring(x); // Create a Ring with a x color
private SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    search = (Button) findViewById(R.id.radar);
    stock = (Button) findViewById(R.id.capsules);
    useRing = (Button) findViewById(R.id.useRing);
    tvRing = (TextView) findViewById(R.id.ring);
    tvNbRing = (TextView) findViewById(R.id.tvNbRing);
    tvRingColor = (TextView) findViewById(R.id.tvRingColor);

    prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
    someint = prefs.getInt("someint", 0);
    someGreen = prefs.getInt("someGreen", 0);
    someRing = prefs.getInt("someRing", 0);
    tvRing.setText("Objets trouvés : " + String.valueOf(someRing));

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            someint = prefs.getInt("someint", 0);
            someGreen = prefs.getInt("someGreen", 0);
            someRing = prefs.getInt("someRing", 0);
            addRing();
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt("someint", redRing);
            editor.putInt("someGreen", greenRing);
            editor.putInt("someRing", bring);
            editor.apply();
            editor.commit();
            lastNumber();
        }
    });

    stock.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentStock = new Intent(MainActivity.this, StockActivity.class);
            startActivity(intentStock);
        }
    });
}

public void addRing() { // I use a method from the class Ring
    r.setNbRingFind();
}

public void lastNumber(){
    prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
    someint = prefs.getInt("someint", 0);
    someGreen = prefs.getInt("someGreen", 0);
    someRing = prefs.getInt("someRing", 0);
    tvRing.setText("Objects find : " + String.valueOf(someint));
}
}

3) Activity2 : StockActivity

import static fr.ringshared.Ring.someint;
import static fr.save.sharedpreferences.Ring.someGreen;
import static fr.save.sharedpreferences.Ring.someRing;

public class StockActivity extends Activity {

TextView tvRing2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_capsules);

    tvRing2 = (TextView) findViewById(R.id.ring2);
    tvGreen = (TextView) findViewById(R.id.green);
    tvTotal = (TextView) findViewById(R.id.total);

    tvRing2.setText("Red rings find : " + String.valueOf(someint));
    tvGreen.setText("Green rings find " + String.valueOf(someGreen));
    tvTotal.setText("All rings find : " + String.valueOf(someRing));
}
}

If someone can help me to find where my code has a problem because in logical no error show, just during running, sometimes the value of the int "redRing" or "someint" has resetted to 0 instead of keep the last number of his value.

回答1:

getSharedPreferences(String name, int mode) call from a Context. If you use context from activity SharedPreferences maybe not found key. You can try getApplicationContext().getSharedPreferences(String name, int mode)