Ich habe diese App, wo ich Sende Absicht von Log In Activity und Empfangen von Absicht für Benutzeraktivität (siehe Anhang), aber wenn ich von anderen Seiten navigieren und dann zu meinem UserActivity dh; Home, Contact Us es ergibt "Nullwerte", ich möchte die Daten in User Activity behalten, auch wenn ich von anderen Seiten navigiere. Hilfe bitte. Vielen Dank!So halten Sie Daten in Aktivität aus der Datenbank
Hier ist meine Codes ..
public class LoginActivityEN AppCompatActivity erstreckt {
private static final String TAG = "LoginActivity";
private static final String URL_FOR_LOGIN = "http://192.168.13.40/android_login_example/login.php";
ProgressDialog progressDialog;
private EditText loginInputUsername, loginInputPassword;
Button btnlogin;
Button btnLinkSignup;
// Session Manager Class
SessionManager session;
//Storing Sessions
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_login_en);
// Session Manager
session = new SessionManager(getApplicationContext());
sharedPreferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_id", "password");
//Bottom Navigation
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_home:
Intent intent1 = new Intent(LoginActivityEN.this, Home.class);
startActivity(intent1);
break;
case R.id.navigation_card:
break;
case R.id.navigation_price:
Intent intent3 = new Intent(LoginActivityEN.this, PriceActivity.class);
startActivity(intent3);
break;
case R.id.navigation_more:
Intent intent4 = new Intent(LoginActivityEN.this, More.class);
startActivity(intent4);
}
return false;
}
});
//Login Activity
loginInputUsername = (EditText) findViewById(R.id.login_input_username);
loginInputPassword = (EditText) findViewById(R.id.login_input_password);
btnlogin = (Button) findViewById(R.id.btn_login);
btnLinkSignup = (Button) findViewById(R.id.registerbutton);
// Progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginUser(loginInputUsername.getText().toString(),
loginInputPassword.getText().toString());
}
});
//Register
btnLinkSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), RegisterEN.class);
startActivity(i);
}
});
}
private void loginUser(final String username, final String password) {
// Tag used to cancel the request
String cancel_req_tag = "login";
progressDialog.setMessage("Logging you in...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
URL_FOR_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String user = jObj.getJSONObject("user").getString("customers_firstname");
String user1 = jObj.getJSONObject("user").getString("customers_lastname");
String user2 = jObj.getJSONObject("user").getString("reward_points");
String user3 = jObj.getJSONObject("user").getString("NoShares");
String user4 = jObj.getJSONObject("user").getString("CardType_ID");
String user5 = jObj.getJSONObject("user").getString("Card_No");
// Launch User activity
Intent intent = new Intent(LoginActivityEN.this, UserActivity.class);
intent.putExtra("customers_firstname", user);
intent.putExtra("customers_lastname", user1);
intent.putExtra("reward_points", user2);
intent.putExtra("NoShares", user3);
intent.putExtra("CardType_ID", user4);
intent.putExtra("Card_No", user5);
startActivity(intent);
finish();
if(username.trim().length() > 0 && password.trim().length() > 0){
session.createLoginSession("username", "password");
Toast.makeText(getApplicationContext(), "Session Active", Toast.LENGTH_SHORT).show();
}
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to login url
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("customers_password", password);
return params;
}
};
// Adding request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
}
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
UserActivity.java
public class UserActivity AppCompatActivity erstreckt {
private TextView greetingTextView;
private TextView totpoints;
private TextView totshare;
private Button btnLogOut;
private ImageView cardshow;
private ImageView bmbc;
private TextView bmbc_text;
SharedPreferences sharedPreferences;
// Session Manager Class
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_user);
// Session class instance
session = new SessionManager(getApplicationContext());
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent intent1 = new Intent(UserActivity.this, Home.class);
startActivity(intent1);
break;
case R.id.navigation_card:
break;
case R.id.navigation_price:
Intent intent3 = new Intent(UserActivity.this, PriceActivity.class);
startActivity(intent3);
break;
case R.id.navigation_more:
Intent intent4 = new Intent(UserActivity.this, PriceActivity.class);
startActivity(intent4);
break;
}
return false;
}
});
greetingTextView = (TextView) findViewById(R.id.greeting_text_view);
totpoints = (TextView) findViewById(R.id.au_tpresult);
totshare = (TextView) findViewById(R.id.au_tsresult);
btnLogOut = (Button) findViewById(R.id.logout_button);
cardshow = (ImageView) findViewById(R.id.card_stack);
Intent intent = getIntent();
String user = intent.getStringExtra("customers_firstname");
String user1 = intent.getStringExtra("customers_lastname");
String user2 = intent.getStringExtra("reward_points");
String user3 = intent.getStringExtra("NoShares");
String user4 = intent.getStringExtra("CardType_ID");
String user5 = intent.getStringExtra("Card_No");
greetingTextView.setText(user + " " + user1);
totpoints.setText(user2);
totshare.setText(user3);
if (user4 == (null)) {
((ImageView) findViewById(R.id.card_stack)).setImageResource(R.color.transparent);
} else if (user4.equals("0")) {
((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_silver);
} else if (user4.equals("1")) {
((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_gold);
}
// Progress dialog
btnLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
session.logoutUser();
Toast.makeText(getApplicationContext(), "Session Ended", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), LoginActivityEN.class);
startActivity(i);
}
});
session.checkLogin();
//CARD NUMB
Hallo in diesem Fall speichern Sie alle Daten in gemeinsamen Präferenz und Zugriff wo immer Sie wollen.Nach erfolgreichem Login anstelle der Weitergabe von Absicht speichern in gemeinsamen Präferenz, dann auf der Homepage abrufen. – Shanmugam
Sie können Ihre Aufgabe mit dem ausführen, was @Shanmugam gesagt hat, oder Sie können diese Variablenwerte in onsaveinstancestate speichern und auf Ihrer on create-Methode wiederherstellen. Stellen Sie sicher, dass die Ansichten, die Sie erneut speichern möchten, Felder von Klassen und nicht lokalen Variablen einer Methode sind. :-) –