共享偏好问题

时间:2013-09-05 07:48:10

标签: android

我创建了一个页面,其中我在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();
    }
});
}
} 

3 个答案:

答案 0 :(得分:1)

我已更新您的代码,它可能对您有所帮助:

 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);

答案 1 :(得分:0)

  • 两者都使用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();
    }

答案 2 :(得分:0)

请您确认一下,您是否正确填充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();
    }
});
}
} 

看看这个。