{"id":35,"date":"2021-11-12T03:44:49","date_gmt":"2021-11-12T03:44:49","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/heymae\/?p=35"},"modified":"2021-11-12T03:44:49","modified_gmt":"2021-11-12T03:44:49","slug":"recyclerview-data-binding-and-parse","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/heymae\/2021\/11\/12\/recyclerview-data-binding-and-parse\/","title":{"rendered":"RecyclerView, Data Binding, and Parse"},"content":{"rendered":"\n<p>Think about the most popular apps that contain a feed, such as Twitter or Instagram. The more you scroll, the more posts you&#8217;re shown. Although the posts have different content, the overall layout of each post is identical to the next, this is RecyclerView. RecyclerView is used to efficiently display large amounts of data on android applications. The entire RecyclerView is known as a ViewGroup, and the individual &#8220;post&#8221; layout is known as View Holder. <\/p>\n\n\n\n<p>The fragment class is where the actual adapter is instantiated, and the ViewModel class is where the actual database call is made. In the example below, we use the Parse Database.<\/p>\n\n\n\n<p>This is the code for a fragment that contains a Recyclerview. You&#8217;ll notice the data binding in the layout file, with the data element in the view root element<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"564\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.05.38-PM.png\" alt=\"\" class=\"wp-image-36\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.05.38-PM.png 730w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.05.38-PM-300x232.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The individual item (e.g. one tweet) has a separate layout file. This is what each individual data item will be binded to. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"526\" height=\"381\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.06.39-PM.png\" alt=\"\" class=\"wp-image-37\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.06.39-PM.png 526w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.06.39-PM-300x217.png 300w\" sizes=\"auto, (max-width: 526px) 85vw, 526px\" \/><\/figure>\n\n\n\n<p>RecyclerView binds the individual view holder to its data, this is done in the adapter class. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"826\" height=\"733\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.08.34-PM.png\" alt=\"\" class=\"wp-image-38\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.08.34-PM.png 826w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.08.34-PM-300x266.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.08.34-PM-768x682.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n\n\n\n<p>The adapter contains the onBindViewHolder function, where the viewHolder is bound to an item. In the above example, we have a list of notes. The onBindViewHolder takes a note at each index (or position) and binds it to the viewHolder. <\/p>\n\n\n\n<p>The fragment class contains a function, onCreateView, where the adapter is instantiated. You&#8217;ll notice it also creates a new savedNoteViewModel.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"983\" height=\"674\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.15-PM.png\" alt=\"\" class=\"wp-image-39\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.15-PM.png 983w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.15-PM-300x206.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.15-PM-768x527.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The savedNoteViewModel sets out &#8220;notes&#8221; to the actual data that it receives from making the database call: <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"146\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.33-PM.png\" alt=\"\" class=\"wp-image-40\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.33-PM.png 695w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.14.33-PM-300x63.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this example, we use the Parse database. This function shown below queries the user&#8217;s saved notes, along with the actual data for each note. Then the list of notes is returned. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"448\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.20.27-PM.png\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.20.27-PM.png 1001w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.20.27-PM-300x134.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.20.27-PM-768x344.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Once you run your app, you should see something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"368\" height=\"667\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.43.40-PM.png\" alt=\"\" class=\"wp-image-42\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.43.40-PM.png 368w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4726\/files\/2021\/11\/Screen-Shot-2021-11-11-at-9.43.40-PM-166x300.png 166w\" sizes=\"auto, (max-width: 368px) 85vw, 368px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>And there you have it! A short example using recyclerView, Data Binding, and Parse<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Think about the most popular apps that contain a feed, such as Twitter or Instagram. The more you scroll, the more posts you&#8217;re shown. Although the posts have different content, the overall layout of each post is identical to the next, this is RecyclerView. RecyclerView is used to efficiently display large amounts of data on &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/heymae\/2021\/11\/12\/recyclerview-data-binding-and-parse\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RecyclerView, Data Binding, and Parse&#8221;<\/span><\/a><\/p>\n","protected":false},"author":11566,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/users\/11566"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/posts\/35\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/heymae\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}