我已经创建了一个页面,其中我接收字符串和int在EditText,然后存储在共享首选项点击保存按钮,但我不能使它的功能。具体来说,当我重新打开页面数据丢失时,不会存储数据。请帮帮忙
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();
}
});
}
} 发布于 2013-09-05 08:01:45
我已经更新了您的代码,它可能会帮助您:
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-05 07:54:13
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-05 08:44:16
请您确认一下,您是否正确地填充了来自SharedPref的数据来加载data.If,给出一个简短的说明。我不能对此发表评论。
编辑:
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();
}
});
}
} 看看这个。
https://stackoverflow.com/questions/18630610
复制相似问题