1

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

Ghiggz Pikkoro
  • 136
  • 1
  • 14

1 Answers1

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();
                    }
                }
            }
    );
}
Hasan Saykın
  • 138
  • 1
  • 8
  • Ok thank u Hasan, I just read it, so I don't need to change anything on the MainActivity right? Because I want the nickname from ProfilActivity (activity2) is displayed on the MainActivity (activity1) – Ghiggz Pikkoro May 04 '17 at 13:09
  • In fact the nickname who the user entered in editText in ProfilActivity, I get the text of editText throws an Intent in order to display this nickname on a TextView in the MainActivity and I want in this TextView the nickname save. Because yo code change the editText in a TextView, I don't want to do it sorry for my bad explanation. – Ghiggz Pikkoro May 04 '17 at 13:25
  • Check out the edited answer. I made some changes in Mainactivity oncreate primitively to load nickname and show it in tv. – Hasan Saykın May 04 '17 at 14:32
  • Ok so I don't need to use the intent in the MainActivity right? And I don't need to change editText in Textview on the ProfilActivity so I write that (I edited my post, can u check it out), but it's not working. – Ghiggz Pikkoro May 04 '17 at 15:23
  • In the main activity when user clicks login you can open ProfilActivity, and after saving user data you can return to main activity. – Hasan Saykın May 04 '17 at 15:30
  • Ok sorry I just finish to edited it now. Maybe I misunderstand – Ghiggz Pikkoro May 04 '17 at 15:35
  • I suspect, your SQL database code does not work, but I'm not sure. Now with my edited code you just save and load nickname with the help of sharedpreferences. See I made bold where you made mistake. – Hasan Saykın May 04 '17 at 15:44
  • Ok I try it, my database can work I just didn't write all code about it – Ghiggz Pikkoro May 04 '17 at 15:59
  • It can work good, but maybe u're right with my database there's something wrong about the nickname, because if the user want to change his nickname, it just changed on the database and not display the changement on the SharedPreferences, but it doesn't matter. i think it's better if I use the text saved in my database instead of get the text write in the EditText. – Ghiggz Pikkoro May 04 '17 at 17:09
  • Thank u for the time u spend for help me. – Ghiggz Pikkoro May 04 '17 at 17:10
  • Just a last question, user can also take a picture in ProfilActivity by clicking a button on this activity then he can see this photo on an ImageView, do u think I can save the display of this photo using SharedPreferences too? If yes how to do with prefs.edit(), there's a method I can use? – Ghiggz Pikkoro May 04 '17 at 17:13
  • I don't know much about saving image(It must be taking the uri of image to be displayed and set that uri to an imageview, I guess, I'm not sure.) but you can search the website there are lots of examples. I'm glad, I helped you. It is not a problem. – Hasan Saykın May 04 '17 at 17:29
  • Ok but for displayed it I have no problem for it, just I want to save the image taken which is display on the imageView, if I quit the app the photo disappears – Ghiggz Pikkoro May 04 '17 at 17:47
  • Yeah. There are topics about it check them out. Sharedpreferences do not save image as far as I know. – Hasan Saykın May 04 '17 at 18:02
  • Ok thanks I will check it, thank u very much for yo help – Ghiggz Pikkoro May 04 '17 at 18:19
  • It's not a problem ;) – Hasan Saykın May 04 '17 at 18:32
  • Hi Hasan, do u know something about how to save value of an int that I put in a TextView? – Ghiggz Pikkoro May 06 '17 at 16:04
  • Hi, first you have to convert string to integer like this: int x = Integer.parseInt(string); Then you should save it in sharedpreferences using getInt putInt. – Hasan Saykın May 06 '17 at 19:02
  • Ok I will try it thank u, now I can't but I will try tomorrow, it's night in France – Ghiggz Pikkoro May 06 '17 at 19:33
  • In fact I convert int to string : tvRed.setText("Nombre de ... : " + String.valueOf(nbRed)); in order to display the value of my INT in the TextView. I try to save this value but all can't work at all. I tried using SharedPreferences with the getInt and putInt but no way, could u help me one last time with SharedPreferences? – Ghiggz Pikkoro May 07 '17 at 21:44
  • SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE); someint = prefs.getInt("someint", "0"); SharedPreferences.Editor editor = prefs.edit(); editor.putInt("someint", nbRed); editor.commit(); – Hasan Saykın May 08 '17 at 12:22
  • Hey thanks for yo answer, just "someone" is which type of variable? an int? Because I have an error on someint = prefs.getInt("someint", "0"); it said wrong second argument – Ghiggz Pikkoro May 08 '17 at 13:34
  • Hasan, I just did a new question : U can see it here : http://stackoverflow.com/questions/43849352/sharedpreferences-save-value-of-int-in-a-textview – Ghiggz Pikkoro May 08 '17 at 13:46
  • Thank u if u could help me again, this is the last time I disturb u, promise – Ghiggz Pikkoro May 08 '17 at 13:46