我参与了存储和获取共享首选项。当用户注册时,如果注册成功,那么这些值必须存储在他的个人资料中,除了我的个人资料类。我不知道为什么会得到默认值给我一个建议。
提前致谢。
请再说一遍我在这里更新了代码。
public class Register extends Activity {
/**
* Defining layout items.
**/
EditText mFirstName, mLastName, mUsername, mMobile, mPassword;
ImageView img_back;
Button btnRegister, mbtnSignIn;
TextView registerErrorMsg;
String jsonresponce=null;
String enteredUsername,enteredPassword,enteredfname,enteredlname,enteredmobile;
String REGISTER_URL;
ProgressDialog pd;
private SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mFirstName = (EditText) findViewById(R.id.fname);
mLastName = (EditText) findViewById(R.id.lname);
mUsername = (EditText) findViewById(R.id.username);
mMobile = (EditText) findViewById(R.id.mobie);
mPassword = (EditText) findViewById(R.id.pword);
btnRegister = (Button) findViewById(R.id.register);
mbtnSignIn = (Button) findViewById(R.id.signin);
registerErrorMsg = (TextView) findViewById(R.id.tv);
sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE);
img_back = (ImageView) findViewById(R.id.imageback);
img_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i_backtohome = new Intent(Register.this, HomeActivity.class);
startActivity(i_backtohome);
}
});
mbtnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i_backtosignin = new Intent(Register.this, Signin.class);
startActivity(i_backtosignin);
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredUsername = mUsername.getText().toString();
Log.e("username",enteredUsername);
enteredPassword = mPassword.getText().toString();
Log.e("username",enteredPassword);
enteredfname = mFirstName.getText().toString();
Log.e("username",enteredfname);
enteredlname = mLastName.getText().toString();
Log.e("username",enteredlname);
enteredmobile = mMobile.getText().toString();
Log.e("username",enteredmobile);
REGISTER_URL = "http://ffhhhjkhjgh/register.php?UserName="+enteredUsername+"&FirstName="+enteredfname+"&LastName="+enteredlname+"&Mobile="+enteredmobile+"&Pass="+enteredPassword;
// REGISTER_URL = REGISTER_URL + enteredUsername + enteredPassword + enteredfname + enteredlname + enteredmobile;
Log.e("finalurl",REGISTER_URL);
if (enteredUsername.equals("") || enteredfname.equals("") || enteredlname.equals("")
|| enteredmobile.equals("") || enteredPassword.equals("")) {
Toast.makeText(Register.this, "All Fields are Mandatory", Toast.LENGTH_LONG).show();
return;
}
if (mUsername.length() <= 1 || mPassword.length() <= 1) {
Toast.makeText(Register.this, "Username or password length must be greater than one", Toast.LENGTH_LONG).show();
return;
}
new AsyncDataClass().execute();
}
});
}
class AsyncDataClass extends AsyncTask<String, String, String> {
/* String fName;
String lName;
public AsyncDataClass(){
this.fName = enteredfname;
this.lName = enteredlname;
}*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(Register.this);
pd.setMessage("Loading...");
pd.setCanceledOnTouchOutside(false);
pd.show();
}
@Override
protected String doInBackground(String... params) {
try{
URL url = new URL(REGISTER_URL);
String res;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
res = conn.getResponseMessage();
Log.e("res", res);
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
try{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
reader.close();
jsonresponce = sb.toString();
Log.e("jsondata", jsonresponce);
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
conn.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}
}catch (Exception e){
e.printStackTrace();
}
return jsonresponce;
}
@Override
protected void onPostExecute(String s) {
pd.dismiss();
if("SUCCESS".equals(jsonresponce)){
Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("enteredfname", enteredfname);
editor.putString("enteredlname", enteredlname);
editor.commit();
}else{
Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
}
super.onPostExecute(s);
}
}
}
答案 0 :(得分:1)
您正在使用getSharedPreferences和getDefaultSharedPreferences。
这样做 -
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("enteredfname", enteredfname);
editor.putString("enteredlname", enteredlname);
editor.commit();
后来 -
SharedPreferences preferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
String value = preferences.getString("enteredfname", "enteredfname");
答案 1 :(得分:0)
尝试这样的事情..创建一个类
public class SharedPreferenceCustom {
private Context mContext;
private String defValue = "";
public SharedPreferenceCustom(Context context) {
this.mContext = context;
}
public void setSharedPref(String inputKey, Object inputValue) {
final SharedPreferences sharedPreferences = mContext.getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(inputKey, String.valueOf(inputValue));
editor.apply();
}
public String getSharedPref(String inputKey) {
SharedPreferences sharedPreferences = mContext.getSharedPreferences("a2a", Context.MODE_PRIVATE);
return sharedPreferences.getString(inputKey, defValue);
}
}
并在需要时致电
通过
打电话 SharedPreferenceCustom sp = new SharedPreferenceCustom(mContext);
sp.setSharedPref("KEY", "VALUE");
// or
sp.getSharedPref("KEY");
答案 2 :(得分:0)
您已使用SharedPreferences.Editor存储值,然后在另一个Activity中使用PreferenceManager来检索值。这就是为什么你总是得到默认值。
PreferenceManager用于存储特定应用的首选项。它们用于存储应用程序设置,其中SharedPreferences用于保存数据或值以在应用程序内部工作。它们不能用于应用程序设置。这是两个单独的文件,但都使用Preferences类。
您需要使用SharedPreferences对象来检索值而不是PreferenceManager。您已在AnotherActivity
内创建了名为“preferences”的SharedPreferences对象。只需使用此SharedPreferences对象来检索您想要的数据,如下所示:
String value = preferences.getString("key", def_value);
答案 3 :(得分:0)
为了节省价值:
@Override
protected void onPostExecute(String s) {
pd.dismiss();
if("SUCCESS".equals(jsonresponce)){
Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
SharedPreferences sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("enteredfname", enteredfname);
editor.putString("enteredlname", enteredlname);
editor.commit();
}else{
Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
}
super.onPostExecute(s);
}
}
要追溯价值:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_myprofile, container, false);
mfname = (EditText)rootView.findViewById(R.id.fname);
mlname = (EditText)rootView.findViewById(R.id.lname);
mcontact = (EditText)rootView.findViewById(R.id.contact);
mpassword = (EditText)rootView.findViewById(R.id.editpass);
SharedPreferences sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE)
value = sharedpreferences.getString("enteredfname", "enteredfname")
mfname.setText(value);
return rootView;
}