 |
Android สอบถามการเก็บค่า Share preference ใหม่เมื่อทำการแก้ไขชื่อโปรไฟล์ |
|
 |
|
|
 |
 |
|
[bตอนนี้ทำการแก้ไขชื่อโปรไฟล์ใน database ได้แล้วค่ะ แต่ว่าชื่อในหน้าโปรไฟล์ยังเป็นชื่อเดิมที่สมัครสมาชิกตอนแรกค่ะ
คือต้องการที่จะเก็บค่าที่แก้ไขนั้นเป็น share preference ใหม่อีกครั้งหลังจากแก้ไขชื่อ เพื่อที่ชื่อใหม่ที่แก้ไขจะไปโชว์ในหน้า
โปรไฟล์ของเราอะคะ
รบกวนด้วยนะคะ ยังไม่ค่อยถนัดจาว่าเท่าไหร่ค่ะ[/b]
Code (Android-Java)
public class ChangeDisplayNameActivity extends Activity {
private static final String PREFERENCE_KEY = null;
private static final String DISPLAY_KEY = null;
private SharedPreferences sh_pref;
private SharedPreferences.Editor sh_edit;
UserPreference setting;
private int user_id;
private String getuser_id;
private String user_display_name;
EditText EDTEditdisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_change_displayname);
// custom my action bar
ActionBar actionbar = getActionBar();
actionbar .setTitle("CHANGE DISPLAY NAME");
// back navigation
actionbar.setDisplayHomeAsUpEnabled(true);
// hide icon app
getActionBar().setDisplayShowHomeEnabled(false);
setting = new UserPreference(ChangeDisplayNameActivity.this);
user_id = setting.getUserID();
getuser_id = String.valueOf(user_id);
Button BTNsaveDisplayname = (Button) findViewById(R.id.BTNsaveDisplayname);
BTNsaveDisplayname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateDisplayName();
}
});
}//end onCreate
public boolean UpdateDisplayName() {
EDTEditdisplay = (EditText) findViewById(R.id.EDTEditdisplay);
// Dialog alert
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// Check Displayname
if(EDTEditdisplay.getText().length() == 0)
{
alert.setMessage("Please input new display name");
alert.show();
EDTEditdisplay.requestFocus();
return false;
}
// gettext from edittext
user_display_name = EDTEditdisplay.getText().toString();
List<NameValuePair> update_display = new ArrayList<NameValuePair>();
update_display.add(new BasicNameValuePair("user_id", getuser_id));
update_display.add(new BasicNameValuePair("user_display_name", user_display_name));
JSONParser jspaser = new JSONParser();
String url = "http://su13540254.2th.asia/Hello_Oppa/update_displayname.php";
String dataJSON = jspaser.makeHttpRequest(url,JSONParser.methodPost, update_display);
Log.d("show : ", dataJSON);
Toast.makeText(getApplicationContext(),
"Change display name, Complete", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ChangeDisplayNameActivity.this, MainActivity.class);
startActivity(intent);
return true;
}//end UpdateDisplayName
Tag : Java, Android, JAVA
|
|
 |
 |
 |
 |
Date :
2015-03-13 21:27:02 |
By :
2keysworld |
View :
1541 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูตัวอย่างนี้ครับ
Code (Android-Java)
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "TGCPrefs";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_USERNAME = "email";
// Email address (make variable public to access from outside)
public static final String KEY_Password = "password";
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String email, String password , boolean facebookCall) {
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_USERNAME, email);
// Storing email in pref
editor.putString(KEY_Password, password);
editor.putBoolean("FB", facebookCall);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status If false it will redirect
* user to login page Else won't do anything
* */
public boolean checkLogin() {
// Check login status
if (!this.isLoggedIn()) {
/* // user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);*/
return false;
} else if (this.isLoggedIn()) {
return true;
}
return false;
}
public boolean isFacebookLoggedId(){
return pref.getBoolean("FB", false);
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_USERNAME, pref.getString(KEY_USERNAME, null));
// user email id
user.put(KEY_Password, pref.getString(KEY_Password, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser() {
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
/*Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);*/
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
|
 |
 |
 |
 |
Date :
2015-03-14 07:23:03 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|