在Android中,如何使用字节数组进行数据绑定?

在Android中,如何使用字节数组进行数据绑定?,android,arrays,Android,Arrays,我知道如何用字符串值进行数据绑定。 但现在我正在按byteArray类型在房间(本地DB)中插入图像 我想在recyclerView中对byteArray进行数据绑定。可能吗 如果不可能,是否有其他方法与图像进行数据绑定? 或者我可以在房间中插入非byteArray类型的图像吗 备忘类 @Entity data class Memo ( @PrimaryKey(autoGenerate = true) val id: Int?, @ColumnInfo(name = "title

我知道如何用字符串值进行数据绑定。 但现在我正在按byteArray类型在房间(本地DB)中插入图像

我想在recyclerView中对byteArray进行数据绑定。可能吗

如果不可能,是否有其他方法与图像进行数据绑定? 或者我可以在房间中插入非byteArray类型的图像吗

备忘类

@Entity
data class Memo (
    @PrimaryKey(autoGenerate = true) val id: Int?,
    @ColumnInfo(name = "title") val title: String?,
    @ColumnInfo(name = "content") val content: String?,
    @ColumnInfo(name = "photo", typeAffinity = ColumnInfo.BLOB) val photo: ByteArray?
) : Serializable {
    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }

    override fun hashCode(): Int {
        return super.hashCode()
    }
}
listView_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="memo"
            type="simplenote.model.Memo" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="0.7">

            <TextView
                android:id="@+id/textView_title"
                style="@style/memo_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{memo.title}" />

            <TextView
                android:id="@+id/textView_content"
                style="@style/memo_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{memo.content}" />

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.3"
            />

    </LinearLayout>

</layout>

MemoListAdapter.kt

class MemoListAdapter (
    private var items: List<Memo>
) : RecyclerView.Adapter<MemoListAdapter.MemoViewHolder>() {

    class MemoViewHolder(private val binding: MemoListItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Memo) {
            binding.apply {
                memo = item
            }
        }
    }

    override fun getItemCount(): Int = items.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MemoViewHolder = MemoViewHolder(
        DataBindingUtil.inflate(
            LayoutInflater.from(parent.context), R.layout.memo_list_item, parent, false )
    )


    override fun onBindViewHolder(holder: MemoViewHolder, position: Int) {
        val item = items[position]
        holder.apply {
            bind(item)
            itemView.tag = item
        }
    }
}
class-MemoListAdapter(
私有变量项:列表
):RecyclerView.Adapter(){
类MemoViewHolder(私有val绑定:MemoListItemBinding):RecyclerView.ViewHolder(binding.root){
趣味绑定(项目:备忘录){
应用{
备忘录=项目
}
}
}
重写getItemCount():Int=items.size
重写CreateViewHolder(父级:ViewGroup,viewType:Int):MemoViewHolder=MemoViewHolder(
充气(
LayoutFlater.from(parent.context),R.layout.memo\u list\u项,parent,false)
)
覆盖BindViewHolder(holder:MemoViewHolder,位置:Int){
val项目=项目[位置]
持有人:申请{
绑定(项目)
itemView.tag=项目
}
}
}

使用@BindingAdapter,我可以使用ImageView对byteArray进行数据绑定

@BindingAdapter("get_thumbnail")
fun getThumbnail(imageView: ImageView, listOfByteArray: ByteArray) {
    imageView.setImageBitmap(BitmapFactory.decodeByteArray(listOfByteArray, 0, listOfByteArray.size))
}
在XML中

<ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:layout_weight="0.33"
        app:get_thumbnail="@{memo.photo[0]}"
        android:contentDescription="@string/app_name"/>

使用@BindingAdapter,我可以使用ImageView对byteArray进行数据绑定

@BindingAdapter("get_thumbnail")
fun getThumbnail(imageView: ImageView, listOfByteArray: ByteArray) {
    imageView.setImageBitmap(BitmapFactory.decodeByteArray(listOfByteArray, 0, listOfByteArray.size))
}
在XML中

<ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:layout_weight="0.33"
        app:get_thumbnail="@{memo.photo[0]}"
        android:contentDescription="@string/app_name"/>