使用BindingAdapter
类型属性时,我的Enum
遇到问题。当我使用Int时,问题不会发生。
我的 BindingAdapter
@BindingAdapter("app:setImage")
fun ImageView.setImage(transactionType: TransactionType) {
when (transactionType) {
TransactionType.EXPENSE -> this.setImageResource(R.drawable.ic_transaction_expense)
TransactionType.REVENUE -> this.setImageResource(R.drawable.ic_transaction_revenue)
}
}
我的枚举
enum class TransactionType {
REVENUE,
EXPENSE
}
我的对象
class Transaction(
val description: String,
val date: Date,
val value: Double,
val transactionType: TransactionType
)
我的布局
...
<data>
<variable name="transaction" type="com.paulobressan.financas.model.Transaction"/>
</data>
...
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@drawable/ic_transaction_expense"
android:id="@id/image_item"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:padding="8dp"
tools:ignore="ContentDescription"
app:setImage="@{transaction.transactionType}"
/>
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.paulobressan.financas.model.Transaction.transactionType file:/home/paulo/data/projects/Financas2/app/src/main/res/layout/item_transaction.xml loc:35:36 - 35:62 ****\ data binding error ****
答案 0 :(得分:0)
确保将模型与数据绑定模型连接。在ViewHolder for RecyclerView项目中,或者在Activity / Fragment的onCreate()中
答案 1 :(得分:0)
我上这堂课。
class TransactionAdapter(private val transaction: List<Transaction>, private val context: Context) :
Adapter<TransactionAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = DataBindingUtil.inflate<ItemTransactionBinding>(LayoutInflater.from(context), R.layout.item_transaction, parent, false)
return ViewHolder(binding)
}
override fun getItemCount(): Int {
return transaction.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val transaction = transaction[position]
holder.bindView(transaction)
}
class ViewHolder(private val binding: ItemTransactionBinding) : RecyclerView.ViewHolder(binding.root) {
fun bindView(transaction: Transaction) {
binding.transaction = transaction
}
}
}
我的活动
class TransactionsActivity : BaseActivity() {
override fun layoutId(): Int {
return R.layout.activity_transactions
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val recyclerView = lista_transacoes_recyclerview
recyclerView.adapter = TransactionAdapter(transactions(), this)
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
}
}