我有以下两个活动,我想将共享首选项中存在的'name'和'subdivision'中的值发送到第二个活动。我能够使用共享首选项获取值,但我无法将该值发送到第二个活动。以下是我的代码......任何人都可以帮我解决这个问题吗?
public class UidScreen extends Activity {
EditText editrexts, edits, edites, editess;
Button buttons;
String userId;
LayoutTwoActivity layoutTwoActivity;
DatapushService dsp = null;
CheckBox chkSignedIn;
SharedPreferences settings;
private static final String PREFRENCES_NAME = "myprefrences";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uid_screen);
editrexts = (EditText) findViewById(R.id.editText1);
edits = (EditText) findViewById(R.id.editText2);
edites = (EditText) findViewById(R.id.editText3);
editess = (EditText) findViewById(R.id.editText4);
chkSignedIn = (CheckBox) findViewById(R.id.chksingin);
//userId = editrexts.getText().toString();
settings = getSharedPreferences(PREFRENCES_NAME,
Context.MODE_PRIVATE);
String name = settings.getString("name", "");
String division = settings.getString("Division", "");
String Subdivision = settings.getString("Subdivison", "");
String range = settings.getString("Range", "");
editrexts.setText(name);
edits.setText(division);
edites.setText(Subdivision);
editess.setText(range);
buttons = (Button) findViewById(R.id.button1);
buttons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//LayoutTwoActivity ly=new LayoutTwoActivity();
//dsp = new DatapushService();
userId = editrexts.getText().toString();
AssetDetails assetDetails = new AssetDetails();
assetDetails.setUserID(userId);
//ly.assetDetails =assetDetails;
//dsp.assetDetails =assetDetails;
Databaseuid ui = new Databaseuid(UidScreen.this);
ui.open();
String Division = ui.getDivision(userId);
String Subdivision = ui.getSubdivision(userId);
String Range = ui.getRange(userId);
edits.setText(Division);
edites.setText(Subdivision);
editess.setText(Range);
if (chkSignedIn.isChecked()) {
showToast("User Name and Password Saved!!!");
SharedPreferences settings = getSharedPreferences(
PREFRENCES_NAME, Context.MODE_PRIVATE);
settings.edit().putString("name", userId).putString("Division",Division).putString("Subdivison", Subdivision).putString("Range", Range).commit();
showToast("Restart App And see it works!!!");
} else {
showToast("Tick keep me logged in!!!");
}
ui.close();
}
});
}
public void opennewactivity(View view) {
// Do something in response to button !! Used to call MainActivity Class on click of the image !
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("str4", userId);
startActivity(intent);
}
private void showToast(String msg) {
Toast.makeText(UidScreen.this, msg, Toast.LENGTH_LONG).show();
}
}
第二项活动:
public class LayoutTwoActivity extends Activity {
String pref,pref2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xml_second);
//SharedPreference settings;
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
pref = settings.getString("name", "");
pref2 = settings.getString("Subdivison",null);
Toast.makeText(getApplicationContext(), pref,
Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:3)
您实际上是从两个不同的偏好文件中读取的。查看this问题。
这些偏好
getSharedPreferences(PREFRENCES_NAME, Context.MODE_PRIVATE);
与这些不同
PreferenceManager.getDefaultSharedPreferences(this);
答案 1 :(得分:3)
尝试此设置共享首页
String userId=yourstring,range=yourstring,subdivision=yourstring;
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(youractivity.this);
Editor edit = settings.edit();
edit.putString("name", userId);
edit.putString("subdivision", subdivision);
edit.putString("range", range);
settings.commit;
获取共享pref
SharedPreferences settins = PreferenceManager
.getDefaultSharedPreferences(Youractivity.this);
pref1=settings.getString("name", "anon");
pref2=settings.getString("subdivision", "anon");
答案 2 :(得分:0)
刚刚做了以下更改并且工作正常,感谢Enrichman
SharedPreferences settings = getSharedPreferences("myprefrences", Context.MODE_PRIVATE);
pref = settings.getString("name", "");
pref2 = settings.getString("Subdivison",null);
答案 3 :(得分:0)
在您的第一个活动中
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("NameOfShared", "Value");
editor.commit();
在您的第二项活动中
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String value=(mSharedPreference.getString("NameOfShared", "Default_Value"));