簡體   English   中英

共享偏好問題

[英]sharedpreferences issue

我創建了一個頁面,其中我在EditText中接收String和int,然后通過單擊save按鈕存儲在sharedpreferences中,但是我無法使其正常運行。 特別是當我重新打開頁面數據丟失時,不會存儲數據。 請幫忙

public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.abc);
   one2five = (Button) findViewById(R.id.btp1);
   save1 = (Button) findViewById(R.id.btps1);
   edtA = (EditText) findViewById(R.id.etA);
   tA = edtA.getText().toString();
   edtB = (EditText) findViewById(R.id.etB);
   tB = edtB.getInputType();
   edtC = (EditText) findViewById(R.id.etC);
   tC = edtC.getInputType();
   edtD = (EditText) findViewById(R.id.etD);
   tD = edtD.getInputType();
   edtE = (EditText) findViewById(R.id.etE);
   tE = edtE.getInputType();
   edtF = (EditText) findViewById(R.id.etF);
   tF = edtF.getInputType();

   one2five.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub
           Intent openg2j = new Intent("com.sport.sport.G2J");

               abcPref = PreferenceManager.getDefaultSharedPreferences(Abc.this);
               abcPref.getInt("filename", 0);

               startActivity(openg2j);
       }
   });
   save1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        abcPref= getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA);
        editor.putInt("field2Data", tB); 
        editor.putInt("field3Data", tC);
        editor.putInt("field4Data", tD);
        editor.putInt("field5Data", tE);
        editor.commit();
    }
});
}
} 

我已更新您的代碼,它可能會幫助您:

 public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.abc);
   one2five = (Button) findViewById(R.id.btp1);
   save1 = (Button) findViewById(R.id.btps1);
   edtA = (EditText) findViewById(R.id.etA);
   tA = edtA.getText().toString();
   edtB = (EditText) findViewById(R.id.etB);
   tB = edtB..getText().toString();
   edtC = (EditText) findViewById(R.id.etC);
   tC = edtC..getText().toString();
   edtD = (EditText) findViewById(R.id.etD);
   tD = edtD..getText().toString();
   edtE = (EditText) findViewById(R.id.etE);
   tE = edtE..getText().toString();
   edtF = (EditText) findViewById(R.id.etF);
   tF = edtF..getText().toString();

// Initialize your sharedpreference here
abcPref = getApplicationContext().getSharedPreferences("filename", 0); // 0 - for private mode



   one2five.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub
           Intent openg2j = new Intent("com.sport.sport.G2J");
// Here is opening sharedpreference which you have edited in save button
               abcPref = Abc.this.getSharedPreferences(filename,0);
               abcPref.getInt("filename", 0);

               startActivity(openg2j);
       }
   });
   save1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        abcPref=  Abc.this.getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA);
        editor.putInt("field2Data", tB); 
        editor.putInt("field3Data", tC);
        editor.putInt("field4Data", tD);
        editor.putInt("field5Data", tE);
        editor.commit();
    }
});
}
}

//從SharedPreferences獲取數據

 abcPref=  <<YourActivityName>>.this.getSharedPreferences(filename,0);

    String str1= abcPref.getString("field1Data", "DefaultValue_If_valueIsNull");
    String str2=abcPref.getString("field2Data", "DefaultValue_If_valueIsNull");
    Log.i("Log cat values checking","str1="+str1 +"  str2="+str2);
  • 使用PreferenceManager.getDefaultSharedPreferences(Abc.this); 用於保存和獲取值或getSharedPreferences(filename,0);

  • 對於整數強制EditText只采用數字。 使用android:inputType="number"

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        abcPref= PreferenceManager.getDefaultSharedPreferences(Abc.this);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA.getText().toString());
        editor.putInt("field2Data", Integer.parseInt(tB.getText().toString())); 
        editor.putInt("field3Data", Integer.parseInt(tC.getText().toString()));
        editor.putInt("field4Data", Integer.parseInt(tD.getText().toString()));
        editor.putInt("field5Data", Integer.parseInt(tE.getText().toString()));
        editor.commit();
    }

您能否請您確認一下,您是否正確填充SharedPref中的數據以加載數據。如果請,請簡要介紹一下。 因此,我無法發表評論。

編輯:

public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.abc);
   one2five = (Button) findViewById(R.id.btp1);
   save1 = (Button) findViewById(R.id.btps1);
   edtA = (EditText) findViewById(R.id.etA);
   edtB = (EditText) findViewById(R.id.etB);
   edtC = (EditText) findViewById(R.id.etC);
   // others..
 }
 public void onResume(){
  super.onResume();
   abcPref= getSharedPreferences(filename,0);
   edtA.setText(abdPref.getString("field1Data", null));
   // set others' like


   one2five.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub
           Intent openg2j = new Intent("com.sport.sport.G2J");

               abcPref = PreferenceManager.getDefaultSharedPreferences(Abc.this);
               abcPref.getInt("filename", 0);

               startActivity(openg2j);
       }
   });
   save1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        tA = edtA.getText().toString();
        tB = edtB.getInputType();
        tC = edtC.getInputType();
        //others..

        abcPref= getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA);
        editor.putInt("field2Data", tB); 
        editor.putInt("field3Data", tC);
        editor.putInt("field4Data", tD);
        editor.putInt("field5Data", tE);
        editor.commit();
    }
});
}
} 

看看這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM