質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

1305閲覧

Preferencesでの数値の保持

syosinsya12

総合スコア69

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/09/08 04:54

編集2016/09/08 08:49

score1の値によってhighScore1_110の値を変更したいです。
それをHighScoreClassで使用したいのですが、今の状態だとscore1の値にかかわらずhighScore1_1
10の値は0のまま変更されていません。
Preferncesの手順がおかしいのでしょうか?

public class Result extends AppCompatActivity { int highScore1_1; int highScore1_2; int highScore1_3; int highScore1_4; int highScore1_5; int highScore1_6; int highScore1_7; int highScore1_8; int highScore1_9; int highScore1_10 ; int highScore2_1; int highScore2_2; int highScore2_3; int highScore2_4; int highScore2_5; int highScore2_6; int highScore2_7; int highScore2_8; int highScore2_9; int highScore2_10; int highScore3_1; int highScore3_2; int highScore3_3; int highScore3_4; int highScore3_5; int highScore3_6; int highScore3_7; int highScore3_8; int highScore3_9; int highScore3_10; boolean back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); Intent intent = getIntent(); int score1 = intent.getIntExtra(Game1.SCORE, 0); int score2 = intent.getIntExtra(Game2.SCORE2, 0); long score3 = intent.getLongExtra(Game3.SCORE3, 0); if (score1 != 0) { TextView result = (TextView) findViewById(R.id.resultScore); result.setText(String.valueOf(score1)); } else if (score3 != 0) { TextView result = (TextView) findViewById(R.id.resultScore); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss.SSS", Locale.US); result.setText(simpleDateFormat.format(score3)); } else { TextView result = (TextView) findViewById(R.id.resultScore); result.setText(String.valueOf(score2)); } SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Result.this); highScore1_1 = preferences.getInt("highscore1_1", 0); highScore1_2 = preferences.getInt("highscore1_2", 0); highScore1_3 = preferences.getInt("highscore1_3", 0); highScore1_4 = preferences.getInt("highscore1_4", 0); highScore1_5 = preferences.getInt("highscore1_5", 0); highScore1_6 = preferences.getInt("highscore1_6", 0); highScore1_7 = preferences.getInt("highscore1_7", 0); highScore1_8 = preferences.getInt("highscore1_8", 0); highScore1_9 = preferences.getInt("highscore1_9", 0); highScore1_10 = preferences.getInt("highscore1_10", 0); if (highScore1_1 < score1) { highScore1_10 = highScore1_9; highScore1_9 = highScore1_8; highScore1_8 = highScore1_7; highScore1_7 = highScore1_6; highScore1_6 = highScore1_5; highScore1_5 = highScore1_4; highScore1_4 = highScore1_3; highScore1_3 = highScore1_2; highScore1_2 = highScore1_1; highScore1_1 = score1; } else if (highScore1_2 < score1 && score1 <= highScore1_1) { highScore1_10 = highScore1_9; highScore1_9 = highScore1_8; highScore1_8 = highScore1_7; highScore1_7 = highScore1_6; highScore1_6 = highScore1_5; highScore1_5 = highScore1_4; highScore1_4 = highScore1_3; highScore1_3 = highScore1_2; highScore1_2 = score1; } else if (highScore1_3 < score1 && score1 <= highScore1_2) { highScore1_10 = highScore1_9; highScore1_9 = highScore1_8; highScore1_8 = highScore1_7; highScore1_7 = highScore1_6; highScore1_6 = highScore1_5; highScore1_5 = highScore1_4; highScore1_4 = highScore1_3; highScore1_3 = score1; }else if (highScore1_4 < score1 && score1 <= highScore1_3){ highScore1_10 = highScore1_9; highScore1_9 = highScore1_8; highScore1_8 = highScore1_7; highScore1_7 = highScore1_6; highScore1_6 = highScore1_5; highScore1_5 = highScore1_4; highScore1_4 = score1; }else if (highScore1_5 < score1 && score1 <= highScore1_4){ highScore1_10 = highScore1_9; highScore1_9 = highScore1_8; highScore1_8 = highScore1_7; highScore1_7 = highScore1_6; highScore1_6 = highScore1_5; highScore1_5 = score1; }else if (highScore1_6 < score1 && score1 <= highScore1_5){ highScore1_10 = highScore1_9; highScore1_9 = highScore1_8; highScore1_8 = highScore1_7; highScore1_7 = highScore1_6; highScore1_6 = score1; }else if (highScore1_7 < score1 && score1 <= highScore1_6){ highScore1_7 = score1; highScore1_8 = highScore1_7; highScore1_9 = highScore1_8; highScore1_10 = highScore1_9; }else if (highScore1_8 < score1 && score1 <= highScore1_7){ highScore1_9 = highScore1_8; highScore1_10 = highScore1_9; highScore1_8 = score1; }else if (highScore1_9 < score1 && score1 <= highScore1_8){ highScore1_10 = highScore1_9; highScore1_9 = score1; }else if (highScore1_10 < score1 && score1 <= highScore1_9){ highScore1_10 = score1; } SharedPreferences.Editor editor = preferences.edit(); editor.putInt("highscore1_1", highScore1_1); editor.putInt("highscore1_2", highScore1_2); editor.putInt("highscore1_3", highScore1_3); editor.putInt("highscore1_4", highScore1_4); editor.putInt("highscore1_5", highScore1_5); editor.putInt("highscore1_6", highScore1_6); editor.putInt("highscore1_7", highScore1_7); editor.putInt("highscore1_8", highScore1_8); editor.putInt("highscore1_9", highScore1_9); editor.putInt("highscore1_10", highScore1_10); editor.commit(); } コード
public class HighScore extends AppCompatActivity { int highScore1_1; int highScore1_2; int highScore1_3; int highScore1_4; int highScore1_5; int highScore1_6; int highScore1_7; int highScore1_8; int highScore1_9; int highScore1_10; int highScore2_1; int highScore2_2; int highScore2_3; int highScore2_4; int highScore2_5; int highScore2_6; int highScore2_7; int highScore2_8; int highScore2_9; int highScore2_10; int highScore3_1; int highScore3_2; int highScore3_3; int highScore3_4; int highScore3_5; int highScore3_6; int highScore3_7; int highScore3_8; int highScore3_9; int highScore3_10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_high_score); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); highScore1_1 = preferences.getInt("highscore1_1", 0); highScore1_2 = preferences.getInt("highscore1_2", 0); highScore1_3 = preferences.getInt("highscore1_3", 0); highScore1_4 = preferences.getInt("highscore1_4", 0); highScore1_5 = preferences.getInt("highscore1_5", 0); highScore1_6 = preferences.getInt("highscore1_6", 0); highScore1_7 = preferences.getInt("highscore1_7", 0); highScore1_8 = preferences.getInt("highscore1_8", 0); highScore1_9 = preferences.getInt("highscore1_9", 0); highScore1_10 = preferences.getInt("highscore1_10", 0); TextView highScore1 = (TextView)findViewById(R.id.highScore1); TextView highScore2 = (TextView)findViewById(R.id.highScore2); TextView highScore3 = (TextView)findViewById(R.id.highScore3); TextView highScore4 = (TextView)findViewById(R.id.highScore4); TextView highScore5 = (TextView)findViewById(R.id.highScore5); TextView highScore6 = (TextView)findViewById(R.id.highScore6); TextView highScore7 = (TextView)findViewById(R.id.highScore7); TextView highScore8 = (TextView)findViewById(R.id.highScore8); TextView highScore9 = (TextView)findViewById(R.id.highScore9); TextView highScore10 = (TextView)findViewById(R.id.highScore10); highScore1.setText(String.valueOf(highScore1_1)); highScore2.setText(String.valueOf(highScore1_2)); highScore3.setText(String.valueOf(highScore1_3)); highScore4.setText(String.valueOf(highScore1_4)); highScore5.setText(String.valueOf(highScore1_5)); highScore6.setText(String.valueOf(highScore1_6)); highScore7.setText(String.valueOf(highScore1_7)); highScore8.setText(String.valueOf(highScore1_8)); highScore9.setText(String.valueOf(highScore1_9)); highScore10.setText(String.valueOf(highScore1_10)); } } コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

Resultクラスでプリファレンスから値を読みだしていないのが原因ではないでしょうか。

また、更新の処理もおかしいですね。
例えば、score1が1位になった時は下記のようにしていますね。
・score1の値を1位の値に代入する。
・1位の値を2位に代入する。
・2位の値を3位に代入する。
以下略
このようにすると全部の値がscore1の値になります。

下記のように低い順位から代入した方がいいですね。
・9位の値を10位に代入する。
・8位の値を9位に代入する。

投稿2016/09/08 05:18

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

syosinsya12

2016/09/08 08:41

キーが一致していないということで本文のように訂正してみました。 とりあえずResultの値をHighScoreで表示することはできたのですが、2位以下がすべて0になってしまいます。どこかまだおかしいでしょうか? Resultで値を読みだしていなかったというのは影響していたのでしょうか?
syosinsya12

2016/09/08 08:51

無事解決できました。ありがとうございます。
guest

0

putIntで指定したkey名と、getIntで指定したkey名が一致していません。

投稿2016/09/08 05:05

dsuzuki

総合スコア1682

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

syosinsya12

2016/09/08 08:42

ありがとうございます。キーを直してみたのですが2位以下がすべて0になってしまいます。 まだどこかおかしなところはありますか?
syosinsya12

2016/09/08 08:51

無事解決できました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問