RecyclerView不显示Firebase(Kotlin)中的数据

时间:2018-12-25 08:27:53

标签: android firebase firebase-realtime-database kotlin firebaseui

有人可以帮助我吗? 我尝试在线上针对android-firebase的课程,但这是一个古老的课程。 因此代码已经更改。我被onBindViewHolderonCreateViewHolder困住了,因为该课程没有。

class UsersAdapter(databaseQuery: DatabaseReference, var context: Context):
    FirebaseRecyclerAdapter<Users, ViewHolder>(
        FirebaseRecyclerOptions.Builder<Users>().setQuery(databaseQuery, Users::class.java).build()
    ) {
    override fun onBindViewHolder(holder: ViewHolder, position: Int, model: Users) {

        var userID = getRef(position).key

        holder.bindView(model, context)

        holder.itemView.setOnClickListener {
            //todo create a popup dialog where user can do
            Toast.makeText(context, "user row click $userID", Toast.LENGTH_LONG).show()
        }


    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {

        var view = LayoutInflater.from(p0.getContext())
            .inflate(R.layout.friendlist_row, p0, false)

        return ViewHolder(view,context)


    }

}

上方是我的adapter的{​​{1}}

RecycleView

上方是我的class ViewHolder(itemView: View, context: Context): RecyclerView.ViewHolder(itemView) { var userNameTxt: String ? = null var userStatusTxt: String ? = null var userProfilePicLink: String ? = null fun bindView(user: Users, context: Context) { var userName = itemView.findViewById < TextView > (R.id.friendlistNameTXID) var userStatus = itemView.findViewById < TextView > (R.id.friendlistStatusTXID) // var userProfilePic = itemView.findViewById<CircleImageView>(R.id.friendlistImageIVID) //set the string so we can pass in the intent userNameTxt = user.usernickname userStatusTxt = user.userstatus // userProfilePicLink = user.thumb_image userName.text = userNameTxt userStatus.text = userStatusTxt // Picasso.get() // .load(userProfilePicLink) // .placeholder(R.drawable.ic_launcher_foreground) // .into(userProfilePic) } }的{​​{1}}

ViewHolder

上面是我使用的myAdapter,里面有class UsersFragment: Fragment() { var mUserDatabase: DatabaseReference ? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup ? , savedInstanceState : Bundle ? ): View ? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_user, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle ? ) { super.onViewCreated(view, savedInstanceState) var linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) mUserDatabase = FirebaseDatabase.getInstance().reference.child("Users") friendlistRVID.setHasFixedSize(true) friendlistRVID.layoutManager = linearLayoutManager friendlistRVID.adapter = UsersAdapter(mUserDatabase!!, context!!) } }

它什么也不会显示,也没有错误。预先谢谢你

1 个答案:

答案 0 :(得分:0)

您是否在适配器中使用了 itemCount

override fun getItemCount(): Int {
        return count
    }