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

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

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

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

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

579閲覧

Androidアプリの表に2つの値を表示したい

mokimokio

総合スコア53

Android

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

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2022/05/16 02:17

現在表の勉強をしております。自分の目標は二つの配列を使って表のなかに二つの値を表示することです。

まず、表の中に1つの値を持たせる実装は達成できました。
イメージ説明

次にこの表の中にそれぞれの人間の好みを当てはめたいと思っています。下の画像が理想形。(合成で作っただけの画像です)

イメージ説明

そこで僕が作成したコードが下記

MainActivity.kt

1 2import androidx.appcompat.app.AppCompatActivity 3import android.os.Bundle 4import androidx.recyclerview.widget.DividerItemDecoration 5import androidx.recyclerview.widget.LinearLayoutManager 6import androidx.recyclerview.widget.RecyclerView 7import org.w3c.dom.Text 8 9class MainActivity : AppCompatActivity() { 10 11 private lateinit var recyclerView: RecyclerView 12 13 override fun onCreate(savedInstanceState: Bundle?) { 14 super.onCreate(savedInstanceState) 15 setContentView(R.layout.activity_main) 16 17 val TextList = listOf( 18 "ボブ", 19 "エミリー", 20 "マイク", 21 "サム", 22 "エドワード" 23 ) 24 val TextList2 = listOf( 25 "アイスクリームが好き", 26 "ハチミツが好き", 27 "運動が好き", 28 "アウトドアが好き", 29 "野球観戦が好き" 30 ) 31 32 recyclerView = findViewById(R.id.RecyclerList) 33 ⭐️recyclerView.adapter = RecyclerAdapter(TextList) 34 ⭐️recyclerView.adapter = RecyclerAdapter(TextList2) 35 recyclerView.layoutManager = LinearLayoutManager(this) 36 37 val dividerItemDecoration = 38 DividerItemDecoration(this , LinearLayoutManager(this).getOrientation()) 39 recyclerView.addItemDecoration(dividerItemDecoration) 40 41 } 42}

RecyclerAdapter.kt

1import android.view.LayoutInflater 2import android.view.ViewGroup 3import androidx.recyclerview.widget.RecyclerView 4 5class RecyclerAdapter(val list: List<String>) : RecyclerView.Adapter<ViewHolderList>() { 6 override fun onCreateViewHolder(parent: ViewGroup , viewType: Int): ViewHolderList { 7 val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_recycler_list, parent, false) 8 return ViewHolderList(itemView) 9 } 10 11 override fun onBindViewHolder(holder: ViewHolderList, position: Int) { 12 holder.characterList.text = list[position] 13 holder.characterList2.text = list[position] 14 } 15 16 override fun getItemCount(): Int = list.size 17}

ViewHolderList.kt

1import android.view.View 2import android.widget.TextView 3import androidx.recyclerview.widget.RecyclerView 4 5class ViewHolderList (item: View) : RecyclerView.ViewHolder(item) { 6 val characterList: TextView = item.findViewById(R.id.Text1) 7 val characterList2: TextView = item.findViewById(R.id.Text2) 8}

activity_main.xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <androidx.recyclerview.widget.RecyclerView 8 android:id="@+id/RecyclerList" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent"/> 11 12</LinearLayout>

item_recycler_list.xml

1<LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:padding="10dp"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="54dp" 11 android:layout_marginLeft="10dp" 12 android:layout_marginRight="10dp" 13 android:layout_marginBottom="0dp"> 14 15 <TextView 16 android:id="@+id/Text1" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="8dp" 20 android:textSize="10sp" /> 21 22 <TextView 23 android:id="@+id/Text2" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_marginTop="23dp" 27 android:textSize="15sp" /> 28 29 </LinearLayout> 30</LinearLayout>

これでエミュレータを実行すると、、、
イメージ説明
このように一つの配列が二つのtextviewに反映してしまうようになってしまいます。自己考察としてMainActivity.ktの⭐️マークであるこの2行
⭐️ recyclerView.adapter = RecyclerAdapter(TextList)
⭐️recyclerView.adapter = RecyclerAdapter(TextList2)

recyclerView.adapterに再代入してしまっているためだろうと考察しているのですが、間違えているところがわかっても。修正方法がわかりません。

どなたか正解コードを教えてください。その正解をもとに勉強していきたいです><

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

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

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

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

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

hoshi-takanori

2022/05/16 03:09

item_recycler_list.xml で LinearLayout を二重にしていて、内側の LinearLayout に android:orientation が指定されてないので horizontal になってますね。内側の LinearLayout は不要では…。
mokimokio

2022/05/16 10:59

ありがとうございます!修正しておきます!
guest

回答1

0

ベストアンサー

【Kotlin】ListViewの使い方 ②テキストを2つ表示する

[Kotlin de Android] RecyclerViewで2行表示するリスト表示してみよう


簡単に修正するのでしたらアダプタにリストを 2 つ持たせてしまうことでしょう。

kotlin

1 recyclerView.adapter = RecyclerAdapter(TextList, TextList2)

kotlin

1class RecyclerAdapter(val list: List<String>, val list2: List<String>) : RecyclerView.Adapter<ViewHolderList>() {

kotlin

1 holder.characterList.text = list[position] 2 holder.characterList2.text = list2[position]

ちゃんと(?)やるならば、行のデータをクラス化して 1 つのリストとして扱うことです。

kotlin

1data class Row(val name: String, val info: String)

kotlin

1 val rowList = listOf( 2 Row("ボブ","アイスクリームが好き"), 3 Row("エミリー","ハチミツが好き"), 4 Row("マイク","運動が好き"), 5 Row("サム","アウトドアが好き"), 6 Row("エドワード","野球観戦が好き") 7 )

kotlin

1 recyclerView.adapter = RecyclerAdapter(rowList)

kotlin

1class RecyclerAdapter(val list: List<Row>) : RecyclerView.Adapter<ViewHolderList>() {

kotlin

1 holder.characterList.text = list[position].name 2 holder.characterList2.text = list[position].info

投稿2022/05/16 07:55

編集2022/05/16 16:28
jimbe

総合スコア12659

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

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

mokimokio

2022/05/16 11:00

ご丁寧にありがとうございます!大変助かりました!😁
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問