0

I need to store the userid in a shared preference and use it in a second activity, but I don't know how to do it. How can I only store the id and recall it in an second activity?

Here is my code:

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {
                        Boolean esito = response.getBoolean("Esito");

                        if (esito) {

                            JSONArray jsonArray = response.getJSONArray("Dati");

                            Log.d("JSON", String.valueOf(esito));

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject dato = jsonArray.getJSONObject(i);

                                String id = dato.getString("id");
                                String fullname = dato.getString("fullname");
                                String username = dato.getString("username");
                                String password = dato.getString("password");


                                //mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "\n\n");

                                startActivity(new Intent(getApplicationContext(),LoggedActivity.class));

                            }
                        } else {

                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }

                }
            }
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40

2 Answers2

1

If you only want to use the data in the second activity then simply use intent to pass data and, Intent has putExtra() method, by using this method you can pass data between activities.

see this,

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {
                        Boolean esito = response.getBoolean("Esito");

                        if (esito) {

                            JSONArray jsonArray = response.getJSONArray("Dati");

                            Log.d("JSON", String.valueOf(esito));

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject dato = jsonArray.getJSONObject(i);

                                String id = dato.getString("id");
                                String fullname = dato.getString("fullname");
                                String username = dato.getString("username");
                                String password = dato.getString("password");


                                //mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "\n\n");
                                Intent intent = new Intent(getApplicationContext(),LoggedActivity.class)
                                intent.putExtra("id",id);
                                startActivity(intent);

                            }
                        } else {

                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }

                }
            }

but if you want really to store data in SharedPreference then use this,

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {
                        Boolean esito = response.getBoolean("Esito");

                        if (esito) {

                            JSONArray jsonArray = response.getJSONArray("Dati");

                            Log.d("JSON", String.valueOf(esito));

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject dato = jsonArray.getJSONObject(i);

                                String id = dato.getString("id");
                                String fullname = dato.getString("fullname");
                                String username = dato.getString("username");
                                String password = dato.getString("password");

                                SharedPreferences.Editor editor = getSharedPreferences(YOUR_SHARED_PEREFENCE_NAME, MODE_PRIVATE).edit();
                                editor.putString("id", id);
                                editor.commit();

                                //mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "\n\n");

                                startActivity(new Intent(getApplicationContext(),LoggedActivity.class));

                            }
                        } else {

                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }

                }
            }
Chandan Sharma
  • 2,803
  • 1
  • 17
  • 25
0
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {
                        Boolean esito = response.getBoolean("Esito");

                        if (esito) {

                            JSONArray jsonArray = response.getJSONArray("Dati");

                            Log.d("JSON", String.valueOf(esito));

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject dato = jsonArray.getJSONObject(i);

                                String id = dato.getString("id");
                                String fullname = dato.getString("fullname");
                                String username = dato.getString("username");
                                String password = dato.getString("password");

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("id", id);
 editor.putString("fullname", fullname);
 editor.putString("username", username);
 editor.putString("password", password);
 editor.commit();
                                //mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "\n\n");

                                startActivity(new Intent(getApplicationContext(),LoggedActivity.class));

                            }
                        } else {

                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }

                }
            }
Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28