Skip to content

Commit e6b01bb

Browse files
committed
populate the recyclerview
1 parent f7edd2f commit e6b01bb

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

firebase-dataconnect/demo/src/main/kotlin/com/google/firebase/dataconnect/minimaldemo/ListItemsActivity.kt

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
package com.google.firebase.dataconnect.minimaldemo
1717

1818
import android.os.Bundle
19+
import android.view.LayoutInflater
1920
import android.view.View
21+
import android.view.ViewGroup
2022
import androidx.activity.viewModels
2123
import androidx.appcompat.app.AppCompatActivity
2224
import androidx.lifecycle.flowWithLifecycle
2325
import androidx.lifecycle.lifecycleScope
26+
import androidx.recyclerview.widget.LinearLayoutManager
27+
import androidx.recyclerview.widget.RecyclerView
28+
import com.google.firebase.dataconnect.minimaldemo.connector.GetAllItemsQuery
2429
import com.google.firebase.dataconnect.minimaldemo.databinding.ActivityListItemsBinding
30+
import com.google.firebase.dataconnect.minimaldemo.databinding.ListItemBinding
2531
import kotlinx.coroutines.flow.collectLatest
2632
import kotlinx.coroutines.launch
2733

@@ -36,6 +42,7 @@ class ListItemsActivity : AppCompatActivity() {
3642
myApplication = application as MyApplication
3743

3844
viewBinding = ActivityListItemsBinding.inflate(layoutInflater)
45+
viewBinding.recyclerView.layoutManager = LinearLayoutManager(this)
3946
setContentView(viewBinding.root)
4047

4148
lifecycleScope.launch {
@@ -57,18 +64,48 @@ class ListItemsActivity : AppCompatActivity() {
5764
viewBinding.statusText.text = "Loading Items..."
5865
viewBinding.statusText.visibility = View.VISIBLE
5966
viewBinding.recyclerView.visibility = View.GONE
67+
viewBinding.recyclerView.adapter = null
6068
} else if (items !== null) {
61-
viewBinding.statusText.text = "Items: $items"
62-
viewBinding.statusText.visibility = View.VISIBLE
63-
viewBinding.recyclerView.visibility = View.GONE
69+
viewBinding.statusText.text = null
70+
viewBinding.statusText.visibility = View.GONE
71+
viewBinding.recyclerView.visibility = View.VISIBLE
72+
val oldAdapter = viewBinding.recyclerView.adapter as? RecyclerViewAdapterImpl
73+
if (oldAdapter === null || oldAdapter.items !== items) {
74+
viewBinding.recyclerView.adapter = RecyclerViewAdapterImpl(items)
75+
}
6476
} else if (exception !== null) {
6577
viewBinding.statusText.text = "Loading items FAILED: $exception"
6678
viewBinding.statusText.visibility = View.VISIBLE
6779
viewBinding.recyclerView.visibility = View.GONE
80+
viewBinding.recyclerView.adapter = null
6881
} else {
6982
viewBinding.statusText.text = null
7083
viewBinding.statusText.visibility = View.GONE
7184
viewBinding.recyclerView.visibility = View.GONE
7285
}
7386
}
87+
88+
private class RecyclerViewAdapterImpl(val items: List<GetAllItemsQuery.Data.ItemsItem>) :
89+
RecyclerView.Adapter<RecyclerViewViewHolderImpl>() {
90+
91+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewViewHolderImpl {
92+
val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
93+
return RecyclerViewViewHolderImpl(binding)
94+
}
95+
96+
override fun getItemCount() = items.size
97+
98+
override fun onBindViewHolder(holder: RecyclerViewViewHolderImpl, position: Int) {
99+
holder.bindTo(items[position])
100+
}
101+
}
102+
103+
private class RecyclerViewViewHolderImpl(private val binding: ListItemBinding) :
104+
RecyclerView.ViewHolder(binding.root) {
105+
106+
fun bindTo(item: GetAllItemsQuery.Data.ItemsItem) {
107+
binding.id.text = item.id.toString()
108+
binding.name.text = item.string
109+
}
110+
}
74111
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
Copyright 2024 Google LLC
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<LinearLayout
20+
xmlns:android="http://schemas.android.com/apk/res/android"
21+
android:id="@+id/constraintLayout"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:orientation="vertical"
25+
android:padding="8dp"
26+
>
27+
28+
<TextView
29+
android:id="@+id/name"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:ellipsize="middle"
33+
style="@style/TextAppearance.Material3.BodyMedium"
34+
/>
35+
36+
<TextView
37+
android:id="@+id/id"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:ellipsize="middle"
41+
android:layout_marginTop="4dp"
42+
style="@style/TextAppearance.Material3.BodySmall"
43+
/>
44+
45+
</LinearLayout>

0 commit comments

Comments
 (0)