具有多个parentColumn和entityColumn的Android Room关系

具有多个parentColumn和entityColumn的Android Room关系,android,android-room,android-database,Android,Android Room,Android Database,假设您有一个房间数据库,其中有两个表(实体),它们使用外键进行关联。(这只是一个简化的示例,因此无需提出新的数据结构:)) 如何在一次查询中检索数据库中的所有信息? 下面的工作几乎和预期的一样,但我也需要添加City列,但如何才能做到这一点? 如何添加多个parentColumn和entityColumn @Query("SELECT * FROM Streets") public abstract LiveData<List<PoJo>> getAllUsers();

假设您有一个房间数据库,其中有两个表(实体),它们使用外键进行关联。(这只是一个简化的示例,因此无需提出新的数据结构:))

如何在一次查询中检索数据库中的所有信息? 下面的工作几乎和预期的一样,但我也需要添加City列,但如何才能做到这一点? 如何添加多个parentColumn和entityColumn

@Query("SELECT * FROM Streets")
public abstract LiveData<List<PoJo>> getAllUsers();

public static class PoJo {
    @Embedded
    private Street street;
    @Relation(parentColumn = "Country", entityColumn = "Country")
    private List<User> mUsers;
}
@Query(“从街道选择*)
公共抽象LiveData getAllUsers();
公共静态类PoJo{
@嵌入
私家街;;
@关系(parentColumn=“Country”,entityColumn=“Country”)
私人博物馆名单;
}

此答案不会解决房间使用问题。我们使用的是SQLite,所以代码需要根据房间的需要进行样式化。此代码在Kotlin中
首先是两个模型类ParentModel和ChildModel

data class ParentModel (
    val title : String = "",
    val children : List<ChildModel>
)

data class ChildModel(
    val image : Int = -1,
    val title : String = ""
)
数据类父模型(
val title:String=“”,
val子项:列表
)
数据类子模型(
val image:Int=-1,
val title:String=“”
)
旁注:我们没有使用图像,因此很容易丢失
现在的两个适配器是父代和子代,没有创造性的命名

class ChildAdapter(private val children : List<ChildModel>)
: RecyclerView.Adapter<ChildAdapter.ViewHolder>(){

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v =  LayoutInflater.from(parent.context)
                  .inflate(R.layout.child_recycler,parent,false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
    return children.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val child = children[position]
    holder.imageView.setImageResource(child.image)
    holder.textView.text = child.title
}


inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){

    val textView : TextView = itemView.child_textView
    val imageView: ImageView = itemView.child_imageView

}
}

class ParentAdapter(private val parents : List<ParentModel>) : RecyclerView.Adapter<ParentAdapter.ViewHolder>(){

private val viewPool = RecyclerView.RecycledViewPool()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.parent_recycler,parent,false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
   return parents.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val parent = parents[position]
    holder.textView.text = parent.title
    holder.recyclerView.apply {
        layoutManager = LinearLayoutManager(holder.recyclerView.context, LinearLayout.HORIZONTAL, false)
        adapter = ChildAdapter(parent.children)
        recycledViewPool = viewPool
    }
}


inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
    val recyclerView : RecyclerView = itemView.rv_child
    val textView:TextView = itemView.textView
}
}
class ChildAdapter(私有val子项:列表)
:RecyclerView.Adapter(){
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val v=LayoutInflater.from(parent.context)
.充气(右布局、儿童回收器、家长、假)
返回视窗支架(v)
}
重写getItemCount():Int{
返回大小
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val child=子对象[位置]
holder.imageView.setImageResource(child.image)
holder.textView.text=child.title
}
内部类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val textView:textView=itemView.child\u textView
val imageView:imageView=itemView.child\u imageView
}
}
类ParentAdapter(private val parents:List):RecyclerView.Adapter(){
private val viewPool=RecyclerView.RecycledViewPool()
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val v=LayoutFlater.from(parent.context)。充气(R.layout.parent\u recycler,parent,false)
返回视窗支架(v)
}
重写getItemCount():Int{
返回大小
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val parent=父项[位置]
holder.textView.text=parent.title
holder.recyclerView.apply{
layoutManager=LinearLayoutManager(holder.recyclerView.context,LinearLayout.HORIZONTAL,false)
adapter=ChildAdapter(parent.children)
recycledViewPool=viewPool
}
}
内部类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val recyclerView:recyclerView=itemView.rv_子项
val textView:textView=itemView.textView
}
}
请注意,在ParentAdapter中,ChildAdapter将绑定信息
现在,XML关联文件首先是父文件,然后是子文件

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2dp"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        style="@style/Base.TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/rv_child"
        android:padding="20dp"
        android:text="Hello World"
        android:textColor="@color/colorAccent"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="20dp"
        android:layout_marginTop="30dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="74dp" />

</RelativeLayout>


以及要调用以获取数据的SQLite代码(我们还没有编写实现视图的活动)

fun getDepartments():列表{
val cursor=writeabledatabase.query(部门、数组(ID、标题)、null、null、,
空,空,ID)
val list=mutableListOf()
如果(光标!=null){
cursor.moveToFirst()
而(!cursor.isAfterLast){
val departmentId=cursor.getLong(cursor.getColumnIndex(ID))
val items=getItems(部门ID)
val d=ParentModel(cursor.getString(cursor.getColumnIndex(TITLE)),items)
列表.添加(d)
cursor.moveToNext()
}
cursor.close()
}
返回列表
}
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2dp"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        style="@style/Base.TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/rv_child"
        android:padding="20dp"
        android:text="Hello World"
        android:textColor="@color/colorAccent"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="20dp"
        android:layout_marginTop="30dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="74dp" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/child_textView"
        android:layout_width="129dp"
        android:layout_height="37dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/darker_gray"
        android:padding="10dp"
        android:text="TextView"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/child_imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ImageView
        android:id="@+id/child_imageView"
        android:layout_width="126dp"
        android:layout_height="189dp"
        android:layout_marginBottom="38dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="42dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/aviator" />

</android.support.constraint.ConstraintLayout>
fun getDepartments(): List<ParentModel>{
    val cursor = writableDatabase.query(DEPARTMENTS, arrayOf(ID, TITLE), null, null,
        null, null, ID)
    val list = mutableListOf<ParentModel>()
    if (cursor != null) {
        cursor.moveToFirst()
        while (!cursor.isAfterLast){
            val departmentId = cursor.getLong(cursor.getColumnIndex(ID))
            val items = getItems(departmentId)
            val d = ParentModel(cursor.getString(cursor.getColumnIndex(TITLE)), items)
            list.add(d)
            cursor.moveToNext()
        }
        cursor.close()
    }
    return list
}