如何保留数据库中的活动中的数据



我有这个应用程序,我成功地从登录活动发送意图并接收用户活动的意图(请参阅附件),但是当我从其他页面导航然后返回我的用户活动时,即;主页, 联系我们 它的结果是"空值",即使我从其他页面导航,我也想将数据保留在用户活动中。请帮忙。谢谢!

成功的商店

空值

这是我的代码..

public class LoginActivityEN 扩展了 AppCompatActivity {

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();
}

用户活动.java

公共类 UserActivity 扩展了 AppCompatActivity {

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

您可以使用许多方法。

  1. 持久性存储(持续时间长,即使在应用程序关闭后):a. 使用 SharedPreferences, b. 使用 Sqlite

  2. 另一种方法是 .to 扩展应用程序类并将其存储在变量中,它一直保留到应用程序关闭。持续时间短,在应用程序会话期间和应用程序关闭期间)

您的方法取决于您的数据生命周期

相关内容