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

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

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

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

922閲覧

RecyclerViewへのデータの入れ方について

akira3213

総合スコア1

Android Studio

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2021/05/05 15:33

前提・実現したいこと

ゲームに参加するプレイヤー名を追加・削除できる画面を作成しています。
入力欄に名前を入力した後、追加というボタンを押すと画面上に追加されていく仕様です。
<ListView>で表示していた時はうまくいったのですが、これをRecycleViewに変えようと試みたところ、アプリが落ちてしまいます。

発生している問題・エラーメッセージ

追加ボタンを押すとアプリが落ちてしまいます。

該当のソースコード

MainActivity.kt

kotlin

1 2import android.content.Intent 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.view.View 6import android.widget.* 7import androidx.recyclerview.widget.LinearLayoutManager 8import androidx.recyclerview.widget.RecyclerView 9 10class MainActivity : AppCompatActivity() { 11 12 private val playerList = ArrayList<String>() 13 private lateinit var adapter: ArrayAdapter<String> 14 15 override fun onCreate(savedInstanceState: Bundle?) { 16 super.onCreate(savedInstanceState) 17 setContentView(R.layout.activity_main) 18 19 adapter = ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, playerList) 20 findViewById<RecyclerView>(R.id.playerList).adapter = MyAdapter(playerList) 21 findViewById<RecyclerView>(R.id.playerList).layoutManager = LinearLayoutManager(this) 22 23 24//ListViewを使用していた時のコード 25// adapter = ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, playerList) 26// findViewById<ListView>(R.id.playerList).adapter=adapter 27 28 findViewById<Button>(R.id.addPlayerBtn).setOnClickListener(addPlayer()) 29 30//ListViewの時は動作していたOnItemLongListener 31// findViewById<RecyclerViewView>(R.id.playerList).onItemLongClickListener = ListIemClickListener() 32 33 34 } 35 36 private inner class addPlayer : View.OnClickListener{ 37 override fun onClick(view: View) { 38 39 val newPlayerName=findViewById<EditText>(R.id.newPlayerName) 40 playerList.add(newPlayerName.text.toString()) 41 adapter.notifyDataSetChanged() 42 newPlayerName.setText("") 43 } 44 } 45}

MyAdapter.kt

kotlin

1 2 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.TextView 7import androidx.recyclerview.widget.RecyclerView 8 9class MyAdapter(private val data: List<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() { 10 11 class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 12 13 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { 14 val itemView = LayoutInflater.from(parent.context) 15 .inflate(R.layout.my_item_view, parent, false) 16 return MyViewHolder(itemView) 17 } 18 19 override fun onBindViewHolder(holder: MyViewHolder, position: Int) { 20 val name = data[position] 21 holder.itemView.findViewById<TextView>(R.id.item_name).text = name 22 } 23 24 override fun getItemCount(): Int { 25 return data.size 26 } 27}

activity_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:orientation="vertical" 13 app:layout_constraintTop_toTopOf="parent"> 14 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_gravity="center" 19 android:orientation="horizontal"> 20 21 <EditText 22 android:id="@+id/newPlayerName" 23 android:layout_width="250dp" 24 android:layout_height="wrap_content" 25 android:hint="名前を入力してください" /> 26 27 <Button 28 android:id="@+id/addPlayerBtn" 29 android:layout_width="wrap_content" 30 android:layout_height="match_parent" 31 android:text="追加" /> 32 33 </LinearLayout> 34 35 <TextView 36 android:layout_width="match_parent" 37 android:layout_height="match_parent" 38 android:padding="6dp" 39 android:scrollbarSize="12sp" 40 android:text="名前を長押しすると削除されます" /> 41 42 <androidx.recyclerview.widget.RecyclerView 43 android:id="@+id/playerList" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" /> 46 47 <Button 48 android:id="@+id/startGame" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:layout_gravity="center" 52 android:text="スタート" /> 53 54 </LinearLayout> 55 56</androidx.constraintlayout.widget.ConstraintLayout>

my_item_view.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:id="@+id/my_item_view" 6 android:layout_width="match_parent" 7 android:layout_height="80dp"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:id="@+id/item_name" 13 android:textSize="24sp" 14 app:layout_constraintTop_toTopOf="parent" 15 app:layout_constraintBottom_toBottomOf="parent" 16 app:layout_constraintEnd_toEndOf="parent" 17 app:layout_constraintStart_toStartOf="parent" /> 18</androidx.constraintlayout.widget.ConstraintLayout>

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

Android Studio 4.1, Windows10

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

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

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

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

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

hoshi-takanori

2021/05/05 16:56

private lateinit var adapter: ArrayAdapter<String> を MyAdapter に変更する必要がありますね。
akira3213

2021/05/06 05:25

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

回答1

0

自己解決

kotlin

1 2 viewAdapter = MyAdapter(playerList) 3 findViewById<RecyclerView>(R.id.playerList).adapter = viewAdapter

とすることで解決しました。

投稿2021/05/06 14:44

akira3213

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問