2017-11-02 16 views
1

ログインアクティビティからインテントを送信し、ユーザアクティビティ(添付ファイル参照)のインテントを受信することができますが、他のページからナビゲートしてからUserAcitivyに戻ります。私は他のページからナビゲートしても、ユーザーアクティビティにデータを保持したいと思っています。助けてください。ありがとうございました!データベースからデータをアクティビティに保存する方法

Successful store

Null values

ここ..

パブリッククラス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(); 
} 

UserActivity.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 
+0

こんにちは、その場合は共有の設定ですべてのデータを保存し、いつでもどこでも好きな場所にアクセスしてください。 – Shanmugam

+0

あなたは@Shanmugamが言ったことであなたの仕事を達成することができます。あるいは、それらの変数値をonsaveinstancestateに保存してon createメソッドでそれらを復元することができます。再表示するビューが、あるメソッドのローカル変数ではないクラスのフィールドであることを確認してください。 :-) –

答えて

0

使用できるアプローチはたくさんあります。

  1. 持続性ストレージ(長時間、アプリを閉じた後でも): 。 SharedPreferencesを使用する。b。アプリが閉じられるまで

  2. その他の方法は、Applicationクラスを拡張し、変数に格納.TOあるSQLiteにも使用し、それが残っている。(短期間、アプリのセッションとアプリの閉鎖時には)

あなたのアプローチは、URデータの寿命に依存します

+0

ベストオプションは共有設定ですが、私は非静的データを参照しています。 – RJDC

関連する問題