0

UIにnullが表示されるか、整数を使用するとデフォルト値も表示されます。ここでは、文字列形式で表示されますplz help

//putting the information in shared preferences
TextView pScore1=(TextView)findViewById(R.id.pScore1f);


SharedPreferences peepsScores2= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor2 =peepsScores2.edit();
String userScore11 = pScore1.getText().toString();
  editor2.putString("userScore11",userScore11);
  editor2.commit();

  //getting it and editing it

  SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
    int u;
    int one =1;
    int newUsrScore1=1;
    String userScore11 = peepsScores2.getString("userScore11",null);
    u=Integer.parseInt(userScore11);
        newUsrScore1 = u+one;
        String newUserScore1  = Integer.toString(newUsrScore1);

    SharedPreferences.Editor editor = peepsScores2.edit();
    editor.putString(newUserScore1, NewUserScore1);
      editor.commit();

    //getting it and displaying it on the UI

    SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
    String userScore11 = peepsScores2.getString("NewuserScore1",null);


  pScore1.setText(" "+userScore11);
4

3 に答える 3

1

この行

editor.putString(newUserScore1, null);

する必要があります

editor.putString("NewuserScore1",newUserScore1);

また、変更をコミットすることを忘れないでくださいeditor.commit();

于 2012-10-31T12:31:50.233 に答える
1

コードにコメントを追加しました。確認してください。

//putting the information in shared preferences
TextView pScore1=(TextView)findViewById(R.id.pScore1f);


SharedPreferences peepsScores2= 

PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor2 =peepsScores2.edit();
String userScore11 = pScore1.getText().toString();
  editor2.putString("userScore11",userScore11);
  editor2.commit();

  //getting it and editing it

  SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
    int u;
    int one =1;
    int newUsrScore1=1;
    String userScore11 = peepsScores2.getString("userScore11",null);
    u=Integer.parseInt(userScore11);
        newUsrScore1 = u+one;
        String newUserScore1  = Integer.toString(newUsrScore1);

    SharedPreferences.Editor editor = peepsScores2.edit();

     //@Praful: here newUserScore1 seems to be integer value and you are storing 
    //null here. I think it it should be 
    //`editor.putString("NewuserScore1", newUsrScore1);`
    editor.putString(newUserScore1, null);

     //@Praful: call commit here
    editor.commit;

    //getting it and displaying it on the UI

    SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
    String userScore11 = peepsScores2.getString("NewuserScore1",null);


  pScore1.setText(" "+userScore11);
于 2012-10-31T12:32:32.400 に答える
0

commit()SharedPreference を使用するときはいつでも、呼び出して変更を保存することを忘れないでください。

    SharedPreferences.Editor editor = peepsScores2.edit();
    editor.putString("NewuserScore1", newUserScore1);
    editor.commit();
于 2012-10-31T12:31:27.533 に答える