<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adx Kimi on Medium]]></title>
        <description><![CDATA[Stories by Adx Kimi on Medium]]></description>
        <link>https://medium.com/@adamgraves?source=rss-191ad99074e9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*elF3-ksJdG_AZQAbJdSjZQ.png</url>
            <title>Stories by Adx Kimi on Medium</title>
            <link>https://medium.com/@adamgraves?source=rss-191ad99074e9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 00:30:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adamgraves/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android DataBinding adapter and Room’s @Embedded and @Relation annotation]]></title>
            <link>https://medium.com/@adamgraves/android-databinding-adapter-and-rooms-embedded-and-relation-annotation-9ae1f3b69929?source=rss-191ad99074e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ae1f3b69929</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[android-room]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <dc:creator><![CDATA[Adx Kimi]]></dc:creator>
            <pubDate>Mon, 26 Nov 2018 15:03:31 GMT</pubDate>
            <atom:updated>2018-11-27T07:31:09.054Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sAFS61M_0o2gO13zJujHUw.jpeg" /></figure><p>So we have a few item we need to display as a list which is retrievable from Room. Coding a full blown RecyclerView adapter might be overkill for it, so what do we do? Enter Databinding adapter and Room’s @Relation and @Embedded annotation. Let’s take a quiz app which we need to display a series of questions. Each question will have four lines of answer. First lets look at our fragment_question.xml layout:</p><pre>fragment_question.xml</pre><pre>&lt;layout xmlns ...&gt;<br>  &lt;data&gt;<br>   &lt;import type = &quot;java.utils.List&quot;/&gt;<br>   &lt;import type = &quot;yourpackage.model.Answer/&gt;</pre><pre>   &lt;variable<br>    &lt;name = &quot;question&quot;<br>    &lt;type = &quot;yourpackage.model.Question/&gt;</pre><pre>   &lt;!-- List&lt;Answer&gt; --&gt;<br>   &lt;variable<br>    &lt;name = &quot;answerList&quot;<br>    &lt;type = &quot;List&amp;lt;Answer&amp;gt;/&gt;<br>  &lt;/data&gt;</pre><p>To create a list to be use by databinding we need to import java.utils.List and its element which is our Room entity Answer and code it like so List&amp;lt;Answer&amp;gt; which translate to List&lt;Answer&gt;. The variable name for this list is answerList. Since we also need a question, import that too and assign it a variable named question.</p><p>The rest of layout consists of one TextView to display the question and one LinearLayout as a container for our answer list.</p><pre>// continue from above code</pre><pre>&lt;androidx.constraintlayout.widget.ConstraintLayout<br> android:layout_width=&quot;match_parent&quot;<br> android:layout_height=&quot;match_parent&quot;&gt;</pre><pre>  &lt;TextView<br>   // the usual thingy<br>   android:text=&quot;@{question.content}&quot;</pre><pre>  &lt;!-- List of answers layout container --&gt;<br>  &lt;LinearLayout<br>   // the usual thingy<br>   app:answer=&quot;@{answerList}&quot;<br>   app:layout=&quot;@{@layout/item_answer}&quot;</pre><p>Where do app:answer and app:layout attribute came from? Those came from our DataBinding adapter. Lets look at it:</p><pre>public class BindingAdapterUtils {</pre><pre> private BindingAdapterUtils{}</pre><pre>@BindingAdapter({&quot;answer, &quot;layout&quot;})<br>public static void setAnswers(LinearLayout view, List&lt;Answer&gt; answers, int layoutId) {<br>view.removeAllViews();<br>LayoutInflater inflater = LayoutInflater.from(view.getContext());<br>if (answers != null) {<br> for (int i = 0; i &lt; answers.size(); i++ {<br>  Answer answer = answers.get(i);<br>  ItemAnswerBinding binding = DataBindingUtil.inflate(inflater,<br>   layoutId, view, true);<br>  binding.setAnswer(answer);</pre><p>This particular BindingAdapter created two new attributes, app:answer that accept List&lt;Answer&gt; and app:layout which accept a layout. ItemAnswerBinding is the layout in the app:layout=&quot;@{@layout/item_answer}&quot; of fragment_question.xml which is as follows:</p><pre>item_answer.xml</pre><pre>&lt;layout xmlns...&gt;<br> &lt;data&gt;<br>  &lt;variable<br>   name = &quot;answer&quot;<br>   type = &quot;yourpackage.model.Answer/&gt;<br> &lt;/data&gt;</pre><pre>&lt;androidx.constraintlayout.widget.ConstraintLayout<br> android:layout_width=&quot;match_parent&quot;<br> android:layout_height=&quot;wrap_content&quot;&gt;</pre><pre> &lt;TextView<br>  // the usual thingy<br>  android:text=&quot;@{answer.content}&quot;</pre><p>As we can see, data source for our item_answer.xml comes from our DataBinding adapter which in turn originated from fragment_question.xml data binding variable answerList. The only thing to do now is to supply data to this variable and also to question variable in fragment_question.xml.</p><p>Lets look at our data. We’ve two entities in our datababase Question and Answer as follows:</p><pre>@Entity<br>public class Question {<br>@PrimaryKey<br>public int id;<br>public String content;</pre><pre> public Question(int id, String content) {<br>  this.id = id;<br>  this.content = content;<br> }<br>}</pre><pre>@Entity<br>public class Answer {<br>@PrimaryKey<br>public int id;<br>public int questionId;<br>public String content;</pre><pre> public Answer(int id, int questionId, String content) {<br>  this.id = id;<br>  this.questionId = questionId;<br>  this.content = content; <br> }<br>}</pre><p>We need <em>one to many </em>relationship between these two entities. There’s a few way we can do this but because one of our data variable is type List and we want to get it with a beautiful method like question.getAnswers() we can use Room’s @Relation combined with @Embedded.</p><p>From @Relation documentation:</p><pre>1. A convenience annotation which can be used in a Pojo to automatically fetch relation entities.</pre><pre>2. The type of the field annotated with Relation must be a List or  Set.</pre><pre>3. Note that @Relation annotation can be used only in Pojo classes, an Entity class cannot have relations.</pre><pre>4. Note that the @Relation annotated field cannot be a constructor parameter, it must be public or have a public setter.</pre><p>From @Embedded documentation:</p><pre>Can be used as an annotation on a field of an Entity or Pojo to signal that  nested fields (i.e. fields of the annotated field&#39;s class) can be referenced directly in the SQL queries.</pre><p>Good. Next, create a Pojo named QuestionAndAnswer.java.</p><pre>public class QuestionAndAnswers {<br> @Embedded<br> public Question question;</pre><pre> @Relation(parentColumn = &quot;id&quot;, entityColumn = &quot;questionid&quot;)<br> public List&lt;Answer&gt; answers;</pre><pre> public QuestionAndAnswers(Question question) {<br>  this.question = question;<br> }<br>}</pre><p>The entity type Answer is inferred from the return type List&lt;Answer&gt; by Room and since we’ve embedded Question entity, Room will construct the object for us so we can have a Dao class like this:</p><pre>@Dao<br>public abstract class QuestionAnswersDao {</pre><pre> @Transaction<br> @Query(&quot;SELECT * FROM question WHERE id = questionId&quot;)<br> public abstract LiveData&lt;QuestionAndAnswers&gt;<br> findQuestionById(int questionId);</pre><pre>}</pre><p>Don’t forget the @Transaction annotation:</p><pre>If the result of the query is a Pojo with Relation fields, these fields are queried separately. To receive consistent results between these queries, you probably want to run them in a single transaction.</pre><p>The last thing to is to wire everything in our QuestionFragment.java where our fragment_question.xml is inflated:</p><pre>...</pre><pre>private FragmentQuestionBinding binding;<br>private int questionId;</pre><pre>@Nullable<br>@Override<br>public View onCreateView(...) {<br> binding = FragmentQuestionBinding.inflate(...)<br> return binding.getRoot();<br>}</pre><pre>@Override<br>public void onViewCreated(...)<br> super.onViewCreated(view, savedInstanceState)<br> viewModel.getQuestionAndAnswers(questionId).observe(this, questionAndAnswers -&gt; { <br> binding.setQuestion(questionAndAnswers.getQuestion());<br> binding.setAnswerList(questionAndAnswers.getAnswers());<br> });</pre><p>That’s it. Happy coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ae1f3b69929" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>