问题
I'm beginner in Android development and I have an issue with save changement with some texts display in some TextViews. But one is more difficult for me. For explain simple, I have 2 activities, Activity1 is the main, Activity2 is the user informations. In Activity1 (Main) the user clic the button ("login") then he go on Activity2 here user can enter his personal informations and he click on a button ("save data") then I add his datas on a database (SQLite), then when the user click on another button ("go") he return on Activity1 I use an intent in order to show his nickname on a TextView in the Activity1 with a "Welcome" + nickname. But when the user quit the app, there's no save, the changement hasn't save with the nickname, but the nickname are always in my database.
I will show u some code: Activity2:
public class ProfilActivity extends Activity {
EditText editNickname; // We just work on the nickname so I just keep it
MyDatabase myDb;
Button bAddData, go;
public SharedPreferences prefs;
private String nicktobesaved;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_profil);
myDb = new MyDatabase(this);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
editNickname = (EditText) findViewById(R.id.pseudo2);
bAddData = (Button) findViewById(R.id.ajouter);
nicktobesaved = prefs.getString("nickname", "");
bAddData.setEnabled(prefs.getBoolean("isEnabled", true) ? true : false);
AddData();
} // End onCreate()
public void changeNickname(View v) {
// I delete this method who passed an intent
}
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nicktobesaved = prefs.getString("nickname", "");
nicktobesaved = editPseudo.getText().toString();
// The following code is just for save data on my database SQLite
boolean isInserted = myDb.insertData(editNickname.getText().toString());
if (isInserted == true) {
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).apply();
bAddData.setEnabled(false);
} else {
Toast.makeText(ProfilActivity.this, "Data not saved", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
Activity1 (Main activity):
public class MainActivity extends Activity {
TextView tv;
Button login;
public SharedPreferences prefs;
private String nickname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
nickname = prefs.getString("nickname", "");
login = (Button) findViewById(R.id.connexion);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentLogin = new Intent(MainActivity.this, ProfilActivity.class);
startActivity(intentLogin);
}
});
tv = (TextView)findViewById(R.id.pseudo);
if(nickname.equals("")) {
tv.setText("Welcome");
} else {
tv.setText("Welcome " + nickname);
}
}
}
Pseudo = nickname in french, Bienvenue = welcome in french, Thank u all for yo help, I hope u could understand my question. Pikkoro
回答1:
I would recommend you to use Sharedpreferences to store basic information, and read some tutorial or documentation about how to save data on Android. Here I made an example using Sharedpreferences. Hope it helps!
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by Hasan on 4.05.2017.
*/
public class ProfilActivity extends Activity {
EditText editName, editNickname;
Button bAddData;
public SharedPreferences prefs;
//To be saved elements in sharedpreferences!!!
private String nicktobesaved, nametobesaved;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_profil);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
editName = (EditText) findViewById(R.id.name);
editNickname = (EditText) findViewById(R.id.pseudo2);
bAddData = (Button) findViewById(R.id.ajouter);
//default values
nametobesaved=prefs.getString("name", "");
nicktobesaved=prefs.getString("nickname", "");
bAddData.setEnabled(prefs.getBoolean("isEnabled", true) ? true : false);
if(!nametobesaved.equals("")) {
editName.setText(nametobesaved);
}
if (!nicktobesaved.equals("")){
editNickname.setText(nicktobesaved);
}
AddData();
} // End onCreate()
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nametobesaved=prefs.getString("name", "");
nicktobesaved=prefs.getString("nickname", "");
nametobesaved= editName.getText().toString();
nicktobesaved= editNickname.getText().toString();
if(nametobesaved.equals("") || nicktobesaved.equals("")){
Toast.makeText(ProfilActivity.this, "Fill all spaces", Toast.LENGTH_LONG).show();
}else {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", nametobesaved);
editor.putString("nickname", nicktobesaved);
editor.commit();
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).commit();
bAddData.setEnabled(false);
}
}
}
);
}
}
After saving nickname to the Sharedpreferences you need to load it in Main activity. It goes like this:
public class MainActivity extends Activity {
public static final int PROFIL_REQ = 1;
public static final String PSEUDO="nickname";
TextView tv;
Button login;
//Sharedpreferences and nick name variables
private SharedPreferences prefs;
private String nickname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.pseudo);
//Load or create sharedpreferences
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
nickname = prefs.getString("nickname", "");
if(nickname.equals("")) {
tv.setText("Welcome");
}else{
tv.setText("Welcome "+ nickname);
}
}
That's because you deleted sharedpreferences editor.
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nicktobesaved = prefs.getString("nickname", "");
nicktobesaved = editPseudo.getText().toString();
//DO NOT DELETE HERE
SharedPreferences.Editor editor = prefs.edit();
editor.putString("nickname", nicktobesaved);
editor.commit();
// The following code is just for save data on my database SQLite
boolean isInserted = myDb.insertData(editNickname.getText().toString());
if (isInserted == true) {
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).apply();
bAddData.setEnabled(false);
} else {
Toast.makeText(ProfilActivity.this, "Data not saved", Toast.LENGTH_LONG).show();
}
}
}
);
}
来源:https://stackoverflow.com/questions/43771605/sharedpreferences-save-a-text-entered-in-an-edittext-and-display-it-in-a-textvie