我正在尝试使用 sharedpreferences 将文本从一个活动的 2 个 edittext 获取到另一个活动的 2 个文本视图。虽然我已经成功地使用了这种方法,但它只有一个编辑文本到一个文本视图。这次我不知道出了什么问题,因为我无法访问我定义的字符串,只能获得我输入的默认值。希望你们能帮助我提前谢谢。
第一项活动:
eActname = (EditText) findViewById(R.id.eACTName);
eActbud = (EditText) findViewById(R.id.eACTBud);
nameData = getSharedPreferences(namefilename, 0);
budData = getSharedPreferences(budfilename, 0);
bCreate = (Button) findViewById(R.id.bCreateActivity);
bCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent o = new Intent(ActivityCreate.this, ActView.class);
String[] actstrings = new String[] {
eActname.getText().toString(),
eActbud.getText().toString() };
o.putExtra("string", actstrings);
switch (v.getId()) {
case R.id.bCreateActivity:
bCreateClick();
break;
}
}
private void bCreateClick() {
// TODO Auto-generated method stub
String namestringData = eActname.getText().toString();
SharedPreferences.Editor editor = nameData.edit();
editor.putString("namesharedString", namestringData);
editor.commit();
String budstringData = eActbud.getText().toString();
SharedPreferences.Editor budeditor = budData.edit();
budeditor.putString("budsharedString", budstringData);
budeditor.commit();
现在是第二个活动的代码:
tvActName = (TextView) findViewById(R.id.tvActName);
tvActBud = (TextView) findViewById(R.id.tvActBud);
Intent o = getIntent();
o.getStringArrayExtra("strings");
tvActName.setText(o.getStringExtra("text"));
tvActBud.setText(o.getStringExtra("text"));
nameData = getSharedPreferences(namefilename, 0);
budData = getSharedPreferences(budfilename, 0);
String [] actdataReturned = {nameData.getString("namesharedString", "Error"), budData.getString("budsharedString", "Error")} ;
tvActName.setText(actdataReturned[0]);
tvActBud.setText(actdataReturned[1]);