0

I'm making a game a game kind of like a trivia game witha separate class and xml for each question I need it to keep score on my main activity. All the activitys xml shows its players name so I decided since only two things Need to be remembered sharedpreferences is perfect I've gotten the usernames to stay and not cause any force close thanks to this website. But for a few hours I've been trying figure out how to make my textview go from 0 to 1 and then from 1 to 2 and so on I have a class and xml that says wrong and one that says right. so inside those classes I want the to go

if(username1==displayedname.getText.toString ())
      PeepsScore.Userscore1+1           

(basically) Sorry for the syntax but without my notes I'm very bad at this I'm teaching myself via Google and YouTube. Basically I set the textxiew in that class to blank and my java puts in whatever there name is so I am looking for an if statement that's realizes that textview is the same as the shared preference username1. which I believe I've done. so I just need to know how to add a point. I also already have userscore1 stored in shared preferences. If there's another idea completely different than an if statement that's fine as well any example code links hints I will take anything at this point Edit(

ok i changed it to the putint() method and here it is

int zero=0;

    SharedPreferences peepsScores= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
    SharedPreferences.Editor editor =peepsScores.edit();

    editor.putInt("userScore1", zero);
    editor.putInt("userScore2", zero);
    editor.putInt("userScore3", zero);
    editor.putInt("userScore4", zero);
    editor.putInt("userScore5", zero);
    editor.putInt("userScore6", zero);
    editor.putInt("userScore7", zero);
    editor.putInt("userScore8", zero);
    editor.commit();

    SharedPreferences peepsScores = PreferenceManager.getDefaultSharedPreferences(this);
    //tryed this
    int userScore1 = peepsScores.getInt("userScore1","u");
    //tryed this
    userScore2 = peepsScores.getInt("userScore2","0");
    //and tryed this
    String userScore3 = peepsScores.getInt("userScore3","0");
    String userScore4 = peepsScores.getInt("userScore4","0");
    String userScore5 = peepsScores.getInt("userScore5","0");
    String userScore6 = peepsScores.getInt("userScore6","0");
    String userScore7 = peepsScores.getInt("userScore7","0");
    String userScore8 = peepsScores.getInt("userScore8","0");

the get int method is underlined no matter what random things i change and then if i delete the firts line of the recieving class peepScore is underlined im been on android dev webiste this website and all over google everything says its as easy as getInt what am i doing wrong

Ill try to figure out adding later i guess when i finaly get this im posting this for all newbs like me to see lol

2
  • 2
    Don't use == to compare strings, it wont work. Use String.equals(String). Aside from that I don't really understand your question. Commented Oct 26, 2012 at 15:17
  • Basically when my class starts that says right answer I want a point added to what player answered I keep finding putInt (somethIng, somethIng) in my research but I don't know how to make it work Commented Oct 26, 2012 at 15:46

1 Answer 1

1

This is how you save an int to sharedPreferences

SharedPreferences mSettings = mContext.getSharedPreferences(SETTINGS_FILE_NAME, 0);
SharedPreferences.Editor editor = mSettings.edit();
        editor.putInt("yourKey", yourInt);
        editor.commit();

As in 'get' a reference to the shared preference, get an editor from the shared preference, then put the int and its Key into the editor. then commit the editor (this will save to disk so you can get it later from another activity or another session).

also as Tim mentioned

if(username1==displayedname.getText.toString ())

is wrong, use

if(username1.equals(displayedname.getText.toString()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.