3

我创建了一个页面,其中我在 EditText 中接收 String 和 int,然后通过单击保存按钮将其存储在 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();
    }
});
}
} 
4

3 回答 3

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);
于 2013-09-05T08:01:45.047 回答
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();
    }
于 2013-09-05T07:54:13.217 回答
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();
    }
});
}
} 

看看这个。

于 2013-09-05T08:44:16.400 回答