更新App后,SharedPreference值确实保存

时间:2018-07-27 11:41:35

标签: android android-sharedpreferences

我已经创建了一个使用sharedpreference存储数字的应用程序,但是每当我重新安装或更新该应用程序时,该数字值都会被删除或检索一个不同于实际数字的值。

发生的事的例子: 分数= 300

显示0或250

为什么会这样?

SharedPreference代码

package com.example.warrenedy.julyfinal;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; 
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;

import pl.droidsonroids.gif.GifImageView;

public class TestScreen extends Activity implements View.OnClickListener{

Button btnAutoEmotionCrush, btnEmotionVictory, btnSolve, btnFlow, btnMediate, btnExercise, btnTestScreen2;
int counter = 0;
TextView text;
ImageView dr0;
ImageView dr1;
ImageView dr2;
ImageView dr3;
ImageView dr4;
ImageView dr5;
ImageView dr6;
ImageView dr7;
ImageView dr8;
ImageView dr9;

SharedPreferences sharedpreferences;

public static final String MyPREFERENCES = "MyPrefs";
public static final String Count = "Count";
public static final String Screen = "Screen";

int cnt = 0;
String screen;

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

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    if ( sharedpreferences.contains(Count)) {
        cnt = sharedpreferences.getInt(Count, 0);
    }

    screen = "";
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Screen,screen);
    editor.commit();

    dr0=(GifImageView) findViewById(R.id.d0);
    dr1=(GifImageView) findViewById(R.id.d1);
    dr2=(GifImageView) findViewById(R.id.d2);
    dr3=(GifImageView) findViewById(R.id.d3);
    dr4=(GifImageView) findViewById(R.id.d4);
    dr5=(GifImageView) findViewById(R.id.d5);
    dr6=(GifImageView) findViewById(R.id.d6);
    dr7=(GifImageView) findViewById(R.id.d7);
    dr8=(GifImageView) findViewById(R.id.d8);
    dr9=(GifImageView) findViewById(R.id.d9);

    btnAutoEmotionCrush = (Button) findViewById(R.id.btnAutoCrush);
    btnEmotionVictory = (Button) findViewById(R.id.btnEmotionVictory);
    btnSolve = (Button) findViewById(R.id.btnSolve);
    btnFlow = (Button) findViewById(R.id.btnFlow);
    btnMediate = (Button) findViewById(R.id.btnMediate);
    btnExercise = (Button) findViewById(R.id.btnExercise);
    btnTestScreen2 = (Button) findViewById(R.id.Test2);

    btnAutoEmotionCrush.setOnClickListener(this);
    btnEmotionVictory.setOnClickListener(this);
    btnSolve.setOnClickListener(this);
    btnFlow.setOnClickListener(this);
    btnMediate.setOnClickListener(this);
    btnExercise.setOnClickListener(this);
    btnTestScreen2.setOnClickListener(this);

    convertToImg(cnt);

}

@Override
public void onClick(View v) {

    if ( v == btnAutoEmotionCrush ) {
        Intent i = new Intent(this, com.example.warrenedy.julyfinal.ui.AllEmotionsActivity.class);
        startActivity(i);
    }
    if ( v == btnEmotionVictory ) {
        Intent i = new Intent(this, Screen141_Emotion_Selection.class);
        startActivity(i);
    }
    else if ( v == btnSolve ) {
        Intent i = new Intent(this, Screen0_Problem_Solver.class);
        startActivity(i);
    }
    else if ( v == btnFlow ) {
        Intent i = new Intent(this, Screen5_Flows_Of_Life.class);
        startActivity(i);
    }
    else if ( v == btnMediate ) {

    }
    else if ( v == btnExercise ) {

    }
    else if ( v == btnTestScreen2 ) {
        Intent i = new Intent(this, TestScreen2.class);
        startActivity(i);
    }
}

public void convertToImg(Integer cnt)
{
    int a = cnt;

    int num=0;
    int digit=0;
    while (a>=1)
    {


        num=a%10;

        a=a/10;

        switch(num)
        {
            case 0:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==1)
                    dr1.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_0_500_clr_624);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_0_500_clr_624);

                break;
            case 1:


                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==1)
                    dr1.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_1_500_clr_625);
                break;
            case 2:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_2_500_clr_654);
                break;

            case 3:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_3_500_clr_648);
                break;
            case 4:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==1)
                    dr1.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_4_500_clr_650);
                break;

            case 5:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_5_500_clr_643);
                break;

            case 6:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_6_500_clr_630);
                break;
            case 7:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_7_500_clr_615);
                break;
            case 8:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_8_500_clr_647);
                break;
            case 9:

                if(digit==0)
                    dr0.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==1)

                    dr1.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==2)
                    dr2.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==3)
                    dr3.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==4)
                    dr4.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==5)
                    dr5.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==6)
                    dr6.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==7)
                    dr7.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==8)
                    dr8.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                if(digit==9)
                    dr9.setImageResource(R.drawable.gold_streak_9_500_clr_617);
                break;

            default:
                Toast.makeText(this,"error", Toast.LENGTH_SHORT).show();

        }

        digit++;
    }
}

public void onResume(){
    if (sharedpreferences.contains(Count)) {
        cnt = sharedpreferences.getInt(Count, 0);
    }
    super.onResume();
}
}

1 个答案:

答案 0 :(得分:0)

SharedPreference被保存在应用程序数据文件夹中的xml文件中,这意味着它取决于应用程序数据的状态,重新安装应用程序意味着清除其数据,除非开发人员希望更新更新,否则不应删除该数据。更新以这样做。您可以提供完整的示例以更好地理解。