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

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

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

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android

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

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

1717閲覧

RecyclerViewをviewBindingに対応させたい

tcrt56

総合スコア1

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android

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

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2020/12/14 08:17

前提・実現したいこと

Realmを使ってスケジュールアプリを作っています。Kotlin Android Extensionsが非推奨になるため、アプリをviewBindingに対応させたいと思っています。EditTextやTextView、ButtonをviewBindingに対応させることは出来たのですが、RecyclerViewをviewBindingに対応させるところでハマってしまい困ってます。

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

MainActivity.ktの12行目にある"import kotlinx.android.synthetic.main.content_main.*"をコメントアウトすると26行目と29行目の"list"という文字が赤字に切り替わります。単純にlistの前にbinding.という文字を追加してもエラーが解消されません。どうすればエラーが解消されるのでしょうか。下記のリンクを参照しながら作業をしているのですが、よく理解出来ていません。対処方法をご教示頂けますと幸いです。
https://medium.com/better-programming/exploring-viewbinding-in-depth-598925821e41

該当のソースコード

MainActivity.kt

package com.example.myscheduler import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import com.example.myscheduler.databinding.ActivityMainBinding import com.google.android.material.snackbar.Snackbar import io.realm.Realm import io.realm.kotlin.where //import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.content_main.* class MainActivity : AppCompatActivity() { private lateinit var realm: Realm private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) //setContentView(R.layout.activity_main) setSupportActionBar(binding.toolbar) realm = Realm.getDefaultInstance() list.layoutManager = LinearLayoutManager(this) val schedules = realm.where<Schedule>().findAll() val adapter = ScheduleAdapter(schedules) list.adapter = adapter binding.fab.setOnClickListener { view -> val intent = Intent(this, ScheduleEditActivity::class.java) startActivity(intent) } adapter.setOnItemClickListener { id -> val intent = Intent(this, ScheduleEditActivity::class.java) .putExtra("schedule_id", id) startActivity(intent) } } override fun onDestroy() { super.onDestroy() realm.close() } }

ScheduleAdapter.kt

package com.example.myscheduler import android.text.format.DateFormat import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import io.realm.OrderedRealmCollection import io.realm.RealmRecyclerViewAdapter class ScheduleAdapter(data: OrderedRealmCollection<Schedule>) : RealmRecyclerViewAdapter<Schedule, ScheduleAdapter.ViewHolder>(data, true) { private var listener: ((Long?) -> Unit)? = null fun setOnItemClickListener(listener: (Long?) -> Unit) { this.listener = listener } init { setHasStableIds(true) } class ViewHolder(cell: View) : RecyclerView.ViewHolder(cell) { val date: TextView = cell.findViewById(android.R.id.text1) val title: TextView = cell.findViewById(android.R.id.text2) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val infrater = LayoutInflater.from(parent.context) val view = infrater.inflate( android.R.layout.simple_list_item_2, parent, false ) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val schedule: Schedule? = getItem(position) holder.date.text = DateFormat.format("yyyy/MM/dd", schedule?.date) holder.title.text = schedule?.title holder.itemView.setOnClickListener { listener?.invoke(schedule?.id) } } override fun getItemId(position: Int): Long { return getItem(position)?.id ?: 0 } }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </com.google.android.material.appbar.AppBarLayout> <include layout="@layout/content_main"/> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@drawable/ic_add"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:id="@+id/list"/> </androidx.constraintlayout.widget.ConstraintLayout>

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

・Android Studio 4.1.1
・kotlin 1.4.21
・androidx.recyclerview:recyclerview 1.1.0

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

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

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

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

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

guest

回答1

0

自己解決

下記のサイトを参考にすることで問題が解決出来ました。
https://stackoverflow.com/questions/53300598/how-to-get-included-xml-binding-in-databinding/53300966

activity_main.xmlの<include layout="@layout/content_main"/>にandroid:id="@+id/contentMain"を追加

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </com.google.android.material.appbar.AppBarLayout> <include layout="@layout/content_main" android:id="@+id/contentMain"/> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@drawable/ic_add"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.ktの26行目と29行目の"list"という文字の前に"binding.contentMain."を追加。

package com.example.myscheduler import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import com.example.myscheduler.databinding.ActivityMainBinding import com.google.android.material.snackbar.Snackbar import io.realm.Realm import io.realm.kotlin.where //import kotlinx.android.synthetic.main.activity_main.* //import kotlinx.android.synthetic.main.content_main.* class MainActivity : AppCompatActivity() { private lateinit var realm: Realm private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) //setContentView(R.layout.activity_main) setSupportActionBar(binding.toolbar) realm = Realm.getDefaultInstance() binding.contentMain.list.layoutManager = LinearLayoutManager(this) val schedules = realm.where<Schedule>().findAll() val adapter = ScheduleAdapter(schedules) binding.contentMain.list.adapter = adapter binding.fab.setOnClickListener { view -> val intent = Intent(this, ScheduleEditActivity::class.java) startActivity(intent) } adapter.setOnItemClickListener { id -> val intent = Intent(this, ScheduleEditActivity::class.java) .putExtra("schedule_id", id) startActivity(intent) } } override fun onDestroy() { super.onDestroy() realm.close() } }

以上で正常に動作することが確認出来ました。

投稿2020/12/15 01:38

tcrt56

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問