【发布时间】:2020-06-30 19:06:50
【问题描述】:
简而言之,我有一个与我的项目相关联的 Firebase 数据库。我正在从这个 Firebase 数据库中检索数据,然后将其放到 TableRow 那里我有一个 RecyclerView,但最后它没有显示在页面上。
这是我可以从 Firebase 成功检索数据的证据
我注意到我在 FirebaseRecyclerAdapter 的两个方法(onCreateViewHolder、onBindViewHolder)不起作用,我的意思是任何 Log.d(TAG, message) 都不会出现在 Logcat 中。
我不知道是什么原因造成的,我对此感到困惑。
我在这里做错了什么,我是 Kotlin 和 Android 的新手,我希望从你那里得到任何有用的提示。随便问什么。
MajorFragment.kt
class MajorFragment : Fragment() {
internal var items: MutableList<Major> = ArrayList()
internal lateinit var adapter: FirebaseRecyclerAdapter<Major, MajorViewHolder>
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_majors, container, false)
val recyclerView = root.findViewById<RecyclerView>(R.id.recycler_data_majors)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity)
retrieveData()
setData(recyclerView)
return root
}
private fun setData(rec_view: RecyclerView) {
val query = FirebaseDatabase.getInstance().reference.child("Majors")
val options = FirebaseRecyclerOptions.Builder<Major>()
.setQuery(query, Major::class.java)
.build()
Log.d("options", options.toString())
adapter = object : FirebaseRecyclerAdapter<Major, MajorViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MajorViewHolder {
Log.d("onCreateViewHolder", "onCreateViewHolder") // doesnt show in Logcat
val majorView = LayoutInflater
.from(parent.context)
.inflate(R.layout.layout_tablerow, parent, false)
return MajorViewHolder(majorView, viewType == 1)
}
override fun onBindViewHolder(holder: MajorViewHolder, positon: Int, model: Major) {
holder.setIsRecyclable(true)
holder.majorTV.text = model.title
holder.freePlacesTV.text = model.free_places
holder.kvotasTV.text = model.kvota
holder.paidPlacesTV.text = model.paid_places
holder.first_exam.text = model.first_sub
holder.second_exam.text = model.sec_sub
holder.third_exam.text = model.third_sub
holder.min_points_first.text = model.first_sub_points
holder.min_points_second.text = model.sec_sub_points
holder.min_points_third.text = model.third_sub_points
Log.d("Major", ""+holder.majorTV.text) // doesnt show in Logcat
}
}
rec_view.adapter = adapter
Log.d("adapter", rec_view.adapter.toString())
}
private fun retrieveData() {
items.clear()
val db = FirebaseDatabase.getInstance()
.reference
.child("Majors")
db.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(error: DatabaseError) {
Log.d("ERROR", "" + error.message)
}
override fun onDataChange(snapshot: DataSnapshot) {
for(majorSnapShot in snapshot.children)
{
val major = majorSnapShot.getValue(Major::class.java)
items.add(major!!)
Log.d("Majors", ""+items.size)
}
}
})
}
override fun onStart() {
if(adapter != null)
adapter.startListening()
super.onStart()
}
override fun onStop() {
if(adapter != null)
adapter.stopListening()
super.onStop()
}
}
fragment_majors.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.majors.MajorsFragment"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:background="#5F8A96" >
<TextView
just a TextView />
<TextView
just a TextView />
<TextView
just a TextView />
<TextView
just a TextView />
<TextView
just a TextView />
<TextView
just a TextView />
</TableRow>
<TableRow android:background="#F0F7F7" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_data_majors"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_margin="8dp"
android:textAlignment="center"
android:textSize="20sp" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
layout_tablerow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout2"
android:background="#F0F7F7"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/majorsTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/style_border_table"
android:gravity="center"
android:text="Математика" />
<TextView
android:id="@+id/freePlacesTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/style_border_table"
android:gravity="center"
android:layout_weight="1"
android:text="20" />
<TextView
android:id="@+id/kvotasTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/style_border_table"
android:gravity="center"
android:layout_weight="1"
android:text="2" />
<TextView
android:id="@+id/paidPlacesTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/style_border_table"
android:gravity="center"
android:layout_weight="1"
android:text="6" />
<LinearLayout
android:id="@+id/exams"
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/style_border_table"
android:gravity="left"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/first_exam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Мат"/>
<TextView
android:id="@+id/second_exam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Инф"/>
<TextView
android:id="@+id/third_exam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Рус"/>
</LinearLayout>
<LinearLayout
android:id="@+id/min_points"
android:orientation="vertical"
android:background="@drawable/style_border_table"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:id="@+id/min_points_first"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:text="39"/>
<TextView
android:id="@+id/min_points_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="44"/>
<TextView
android:id="@+id/min_points_third"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:text="45"/>
</LinearLayout>
</LinearLayout>
【问题讨论】:
标签: android kotlin firebase-realtime-database android-recyclerview