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

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

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

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

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

Android

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

Android Studio

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

Q&A

0回答

550閲覧

onCreateでSharedPrefferenceから値を取得してRecyclerViewに表示する

Jirotaro

総合スコア0

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

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

Android

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

Android Studio

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

0グッド

0クリップ

投稿2021/05/08 02:42

編集2021/05/08 06:47

実現したいこと

はじめまして

androidstudio(Java)で簡易なスコアリングアプリ(ゴルフ系)を作っている初学者です。

実現したいことは以下の機能の実装です。
至らないところが多々あるかと思いますが、何卒よろしくお願いいたします。

onCreateでSharedPrefferenceから値を取得してRecyclerViewに表示する

現在は「メニュー画面」(アプリ起動時の最初の画面)から「設定」ボタンをクリックしてプレイヤーの名前を登録する画面(ユーザー名を予め登録しておく機能)を作成して、以下の機能までは実現できています。

・EditTextに入力された名前をSharedPreferencesを使って保持する
(EditText横に配置した登録ボタンを押すことで保持)

・SharedPreferencesで保持した名前をRecyclerViewに表示する

以上の様な機能まではなんとか実現できていますが、プレイヤーの名前を登録してから、「設定」画面に配置した「戻る」ボタンでメニュー画面に遷移し、また「設定」ボタンをクリックしても当然ながら、先ほど登録していたプレイヤーの名前はRecyclerViewに保持されていません。

基本的には「設定」画面で行ったような考え方、コードの書き方で、**「onCreateでSharedPrefferenceから値を取得してRecyclerViewに表示する」**ということが可能になるかなと考えているのですが、どこにどのようなコードを書けば実現できるかがわからないので、ご回答頂けると幸いです。

onCreateの配下にaddUserで行ったことを少し改変して書いていけばいいのでないかと考えているのですが。。。

下記にコードを明記します(他に必要な箇所があればご教授ください)

よろしくお願い致します。

SettingActivity.java ↓

public class SettingActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); } public void openMain(View view) { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } public void addUser(View view) { EditText editTextName = findViewById(R.id.editTextName); String name = editTextName.getText().toString(); SharedPreferences pref = view.getContext().getSharedPreferences("name_list_row", Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putString("stringValue", name); editor.commit(); String stringValue = pref.getString("stringValue", ""); Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show(); RecyclerView recyclerView = findViewById(R.id.recycler_view_user_names); recyclerView.setHasFixedSize(true); RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(rLayoutManager); List<String> userNameList = new ArrayList<>(); userNameList.add(stringValue); UserNameAdapter adapter = new UserNameAdapter(userNameList); recyclerView.setAdapter(adapter); } }

・activity_setting.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SettingActivity"> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="128dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="@+id/guideline3" tools:layout_editor_absoluteX="613dp"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView9" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/editTextName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" android:text="Name" /> <Button android:id="@+id/button_add_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登録する" android:onClick="addUser" /> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view_user_names" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:text="戻る" app:layout_constraintBottom_toTopOf="@+id/guideline3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" android:onClick="openMain"/> </androidx.constraintlayout.widget.ConstraintLayout>

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

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

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

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

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

hoshi-takanori

2021/05/08 09:54

RecyclerView がある Activity 側で、SettingActivity から戻った時 (だけじゃないけど) には onResume が呼ばれるので、そのタイミングで SharedPreferences が更新されてないかチェックすれば良いのでは。
Jirotaro

2021/05/08 10:20

ご回答ありがとうございます。 すみません、まだまだ学習量が足りておらず理解できていないのですが、「SharedPreferences が更新されてないかチェック」というのはどのような書き方になるのでしょうか? ご回答いただけますと幸いです。
hoshi-takanori

2021/05/08 10:55

実際に値を取得して、前回の値と比べることになります。または、変更されたかどうかに関わらず表示を更新してしまうというのもありかも…。
Jirotaro

2021/05/08 11:11

承知しました。ありがとうございます。 考えて書いてみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問