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

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

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

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

Android Studio

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

Q&A

0回答

1603閲覧

【Android】ListViewのDataBinding

taeyang_88

総合スコア10

Android

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

Android Studio

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

0グッド

0クリップ

投稿2021/02/27 04:53

編集2021/02/27 14:09

前提・実現したいこと

ListViewのDataBinding

発生している問題

下記サイトを参考にコーディングしました。
参考サイト

  • 【アプリ構成】

MainActivity(ボタン押下で遷移) → SubActivity(ListView)
ListViewにデータが表示されません。
※Exceptionは発生していません

該当のソースコード

  • データクラス

Java

1/** 2 * 名前のデータクラス 3 */ 4public class Person { 5 6 private String name; 7 private String hobby; 8 9 public Person() { 10 this.name = "太郎"; 11 this.hobby = "クリケット"; 12 } 13 14 public String getName() { 15 return category; 16 } 17 18 public String getHobby() { 19 return url; 20 }
  • ListViewに表示する1要素のレイアウト

xml

1view_person_list.xml 2 3<layout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto"> 5 6 <data> 7 <variable name="person" 8 type="com.example.app.Person" /> 9 </data> 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:padding="10dp" 15 android:orientation="vertical"> 16 17 <TextView 18 android:id="@+id/txt_name" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 app:layout_constraintTop_toBottomOf="parent" 22 android:text="@{person.name}" 23 android:textSize="24sp"/> 24 25 <TextView 26 android:id="@+id/txt_hobby" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 app:layout_constraintTop_toBottomOf="@+id/txt_name" 30 android:text="@{person.hobby}" 31 android:textSize="24sp"/> 32 33 </LinearLayout> 34</layout>

Java

1/** 2 * Adapter作成 3 */ 4public class PersonAdapter extends ArrayAdapter<Person> { 5 6 public PersonAdapter(Context context, List<Person> personList) { 7 super(context, 0, personList); 8 } 9 10 @NonNull 11 @Override 12 public View getView(final int position, View convertView, @NonNull ViewGroup parent) { 13 ViewPersonListBinding binding; 14 15 if (convertView == null) { 16 LayoutInflater inflater = LayoutInflater.from(getContext()); 17 binding = DataBindingUtil.inflate( 18 inflater, R.layout.view_person_list, parent, false); 19 20 convertView = binding.getRoot(); 21 convertView.setTag(binding); 22 } else { 23 binding = (ViewPersonListBinding) convertView.getTag(); 24 } 25 26 binding.setPerson(getItem(position)); 27 28 return binding.getRoot(); 29 } 30 31 32 33public class PersonListView extends ListView { 34 35 PersonAdapter adapter; 36 37 public PersonListView(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 41 // 自動セッター 42 public void setList(List<Person> personList){ 43 44 if (getAdapter() == null) { 45 adapter = new PersonAdapter(getContext(), personList); 46 setAdapter(adapter); 47 48 adapter.notifyDataSetChanged(); 49 } 50 // データ更新 51 adapter.notifyDataSetChanged(); 52 } 53}

xml

1activity_sub.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<layout xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto"> 6 7 <data> 8 <import type="java.util.List" /> 9 <import type="com.example.app.Person" /> 10 <variable 11 name="personList" 12 type="List&lt;Person&gt;" /> 13 </data> 14 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:orientation="vertical"> 19 20 <com.example.app.view.PersonListView 21 android:layout_width="match_parent" 22 android:layout_height="match_parent" 23 android:fadeScrollbars="false" 24 app:list="@{personList}" /> 25 26 </LinearLayout> 27</layout>

Java

1public class SubActivity extends AppCompatActivity { 2 3 private final String TAG = this.getClass().getSimpleName(); 4 5 @Override 6 protected void onCreate(@Nullable Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 Log.i(TAG, "onCreate()"); 9 10 // DataBinding 11 ActivityPersonBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_sub); 12     setContentView(binding.getRoot()); 13 14 List<Person> personList = new ArrayList<>(); 15 personList.add(new Person()); // コンストラクタで name = "太郎", hobby = "サッカー" 設定 16 17 binding.setPersonList(personList); 18 }

試したこと

デバッグで表示したいデータ(name,hobby)の流れを見てみたところ、PersonAdapter#getView()にデータが渡せていないと思われます。

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

  • 環境 

OS:mac
AndroidStudio4.0

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

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

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

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

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

hoshi-takanori

2021/02/27 11:33

とりあえず次の 2 点を修正すれば表示されるかと。 ・SubActivity の onCreate で setContentView(binding.getRoot()); する ・view_person_list.xml が ConstraintLayout で android:layout_height="wrap_content" なのに制約がないため、高さが 0 になってるので、適切な制約を付けるか、LinearLayout などに変更する (PersonListView を作るのはどうかと思うなど、他にも突っ込みどころはありますが…。)
taeyang_88

2021/02/27 14:03

ご指摘の通り修正すると、表示することができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問