問題
タイトルのままです。
DataBindingと式言語(バインディング式)については Android Developers-レイアウトとバインディング式 が詳しいです.
環境
- Android Studio 4.1.2
- Kotlin version 1.5.10
- Android Gradle Plugin 4.1.2
ソース
まず、呼び出すメソッドは以下のEvent#formatMessage
Event.kt
@Serializable
data class Event(
@SerialName("n")
val name: String,
@SerialName("t")
val message: String
) {
override fun toString(): String {
return "- $name\n ${formatMessage("\n ")}"
}
fun formatMessage(separator: String = "\n"): String {
val lines = message.split("[br]", "<hr>")
return lines.joinToString(separator = separator)
}
}
呼び出すバインディングレイアウトはこちら. android:text="@{event.formatMessage()}"
の部分が問題でビルドが通りません.
list_choice_item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="event"
type="jp.seo.uma.eventchecker.model.Event" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp">
<ImageView
android:id="@+id/img_choice_symbol"
android:padding="2dp"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_uma_symbol"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_choice_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
tools:text="選択肢の名前"
android:text="@{event.name}"
android:textSize="@dimen/text_size_overlay"
android:textStyle="bold"
android:textColor="@color/text_gray"
android:layout_marginStart="3dp"
app:layout_constraintStart_toEndOf="@id/img_choice_symbol"
app:layout_constraintTop_toTopOf="@id/img_choice_symbol"
app:layout_constraintBottom_toBottomOf="@id/img_choice_symbol" />
<TextView
android:id="@+id/text_choice_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/text_choice_name"
tools:text="選択時の効果説明"
android:text="@{event.formatMessage()}"
android:textSize="@dimen/text_size_overlay"
android:textStyle="normal"
android:textColor="@color/text_gray"
app:layout_constraintStart_toStartOf="@id/text_choice_name"
android:layout_marginTop="2dp"
app:layout_constraintTop_toBottomOf="@id/text_choice_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ログ
importできないとbuild以下のファイルで怒られますが、そんなの知らん!これだけでは原因が分かりにくい…
エラー: シンボルを見つけられません
import jp.seo.uma.eventchecker.databinding.ListChoiceItemBindingImpl;
^
シンボル: クラス ListChoiceItemBindingImpl
場所: パッケージ jp.seo.uma.eventchecker.databinding
FAILURE: Build failed with an exception.
解決
Kotlinで便利なデフォルト引数ですが、バインディング式では使えないようです. 文字リテラルはバッククオートで囲みます.
list_choice_item.xml
<TextView
android:id="@+id/text_choice_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/text_choice_name"
tools:text="選択時の効果説明"
- android:text="@{event.formatMessage()}"
+ android:text="@{event.formatMessage(`\n`)}"
android:textSize="@dimen/text_size_overlay"
android:textStyle="normal"
android:textColor="@color/text_gray"
app:layout_constraintStart_toStartOf="@id/text_choice_name"
android:layout_marginTop="2dp"
app:layout_constraintTop_toBottomOf="@id/text_choice_name" />