android - 如何在 Kotlin 中使用 Firebase 实时数据库的代码段中显示 RecyclerView
问题描述
我对从 Firebase 获取数据的 RecyclerView 有疑问。
我知道有很多类似的问题,但我找不到这个问题的正确解决方案。我想我有足够的代码来做属于的事情,但有些东西不起作用。当我打开我的应用程序时,我收到错误:
"E / RecyclerView: No adapter attached; skipping layout".
这是我的片段类:
class ExploreFragment : Fragment() {
var fragmentView : View? = null
var mSearchText : EditText? = null
var mRecyclerView : RecyclerView? = null
var mButton : ImageButton? = null
var mDatabase : DatabaseReference? = null
var FirebaseRecyclerAdapter : FirebaseRecyclerAdapter<Users1 , UsersViewHolder>? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = LayoutInflater.from(activity).inflate(R.layout.fragment_explore, container, false)
mSearchText = view?.findViewById(R.id.search_edit_text)
mButton = view?.findViewById(R.id.search_btn)
mRecyclerView = view?.findViewById(R.id.recycler_view_search)
mRecyclerView?.setHasFixedSize(true)
mRecyclerView?.layoutManager = LinearLayoutManager(context)
mDatabase = FirebaseDatabase.getInstance().getReference("Users")
mSearchText?.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
val searchText = mSearchText?.getText().toString().trim()
loadFirebaseData(searchText)
}
} )
return fragmentView
}
private fun loadFirebaseData(searchText : String) {
if(searchText.isEmpty()){
FirebaseRecyclerAdapter?.cleanup()
mRecyclerView?.adapter = FirebaseRecyclerAdapter
}else {
val firebaseSearchQuery = mDatabase?.orderByChild("username")?.startAt(searchText)?.endAt(searchText + "\uf8ff")
FirebaseRecyclerAdapter = object : FirebaseRecyclerAdapter<Users1, UsersViewHolder>(
Users1::class.java,
R.layout.layout_list,
UsersViewHolder::class.java,
firebaseSearchQuery
){
override fun populateViewHolder(viewHolder: UsersViewHolder, model: Users1?, position: Int) {
viewHolder.mview.userName.setText(model?.name)
}
}
mRecyclerView?.adapter = FirebaseRecyclerAdapter
}
}
companion object {
@JvmStatic
fun newInstance() =
ExploreFragment().apply {
arguments = Bundle().apply {}
}
}
class UsersViewHolder(var mview : View) : RecyclerView.ViewHolder(mview) {
}
}
这是我的布局:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar_layout_search">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="@android:color/white"
android:id="@+id/search_toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search_edit_text"
android:layout_width="297dp"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="13dp"
android:layout_marginRight="60dp"
android:layout_toStartOf="@+id/search_btn"
android:background="@drawable/search_layout"
android:ems="20"
android:hint="Search here"
android:inputType="textPersonName"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:textColor="#999999"
android:textSize="10sp" />
<ImageButton
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/search_edit_text"
android:layout_alignBottom="@+id/search_edit_text"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="34dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="-1dp"
android:background="@android:color/background_light"
app:srcCompat="@mipmap/search_button" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPagerImageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="80dp"
android:paddingEnd="80dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar_layout_search">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
这是我的 Users1 类:
class Users1 {
var name: String? = null
var image: String? = null
constructor() {}
constructor(name: String?, image: String?) {
this.name = name
this.image = image
}
}
解决方案
推荐阅读
- spring-boot - 选择spring data jpa 2.4.5时的案例
- javascript - 根据屏幕大小重新定位元素
- rxjs - rxjs 定期或定时器到期时调用函数
- javascript - 带有动画的 JS 的上一个和下一个按钮不起作用
- ethereum - 如何检查是否曾经发送过 ETH 交易
- javascript - 为每个测试获取新的功能实例
- xcode - 在 xcode 12 中创建 ios 15 设备
- python - 显示输出时使用opencv显示更大的图像尺寸
- javascript - 如何在上下文 React 中传递状态
- angular - 将生成的 JS 文件从动态导入移动到不同的构建目录