具有多个 parentColumn 和 entityColumn 的 Android 房间关系
Posted
技术标签:
【中文标题】具有多个 parentColumn 和 entityColumn 的 Android 房间关系【英文标题】:Android Room Relations with multiple parentColumn and entityColumn 【发布时间】:2019-02-06 14:48:50 【问题描述】:假设您有一个 Room 数据库,其中包含两个使用外键关联的表(实体)。 (这只是一个简化的例子,所以不需要提出新的数据结构:))
@Entity(tableName = "Streets", primaryKeys = "Country", "City")
public class Street
...
@Entity(tableName = "Users",
primaryKeys = "Country", "City", "Name",
foreignKeys = @ForeignKey(entity = Street.class,
parentColumns = "Country", "City",
childColumns = "Country", "City",
onDelete = CASCADE))
public class User
...
如何在一次查询中检索数据库中的所有信息? 下面的工作几乎与预期一样,但我还需要添加 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;
【问题讨论】:
【参考方案1】:此答案不会解决使用 Room 的问题。我们使用的是 SQLite,因此代码需要为 Room 设置样式。这段代码在 Kotlin 首先是两个模型类 ParentModel 和 ChildModel
data class ParentModel (
val title : String = "",
val children : List<ChildModel>
)
data class ChildModel(
val image : Int = -1,
val title : String = ""
)
旁注我们没有使用图像,因此很容易松动 现在这两个Adapter YEA Parent和Child没有创意命名
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
注意在 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_
android:layout_
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_
android:layout_
android:orientation="vertical">
<TextView
android:id="@+id/textView"
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_>
<TextView
android:id="@+id/child_textView"
android:layout_
android:layout_
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_
android:layout_
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>
以及调用获取数据的SQLite代码(我们还没有编写Activity来实现视图)
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
【讨论】:
以上是关于具有多个 parentColumn 和 entityColumn 的 Android 房间关系的主要内容,如果未能解决你的问题,请参考以下文章