【问题标题】:Trying to fetch Firebase Cloud Database data and show it in RecyclerView in a fragment尝试获取 Firebase 云数据库数据并将其显示在 RecyclerView 中的片段中
【发布时间】:2021-11-07 10:03:17
【问题描述】:

我正在尝试从 Firebase Cloud Datastore 获取 RecyclerView 中的数据。但是当我这样做时,我收到以下错误:

java.lang.RuntimeException:无法反序列化对象。类 models.Post 没有定义无参数构造函数。如果你使用 ProGuard,请确保这些构造函数没有被剥离

这是我正在使用的片段:

class forumFragment : Fragment(R.layout.fragment_forum)  {

    private lateinit var myadapter: MyAdapter

    private lateinit var fragmentForumBinding: FragmentForumBinding

    private lateinit var userArrayList: ArrayList<itemViewModel>
    private lateinit var postDao: postDao



    //val mAuth= Firebase.auth.currentUser
    val mAuth : FirebaseUser? = FirebaseAuth.getInstance().currentUser

    //private lateinit var recyclerView:RecyclerView



    private var mDocReference: DocumentReference ?=null
    //= FirebaseFirestore.getInstance().collection(/).document()
    /*override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       // recyclerView = view?.findViewById(R.id.recyclerViewForum)!!
        val binding = FragmentForumBinding.inflate(layoutInflater)
        fragmentForumBinding = binding

        setUpRecyclerView()
    }*/

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val binding = FragmentForumBinding.bind(view)
        fragmentForumBinding = binding

        setUpRecyclerView()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view: View = inflater.inflate(R.layout.fragment_forum, container, false)

        val fabButtom = view.findViewById<FloatingActionButton>(R.id.fabSave)

        fabButtom.setOnClickListener{
            if (mAuth !=null){
                fabButtom.visibility = View.INVISIBLE
                fragmentManager?.beginTransaction()?.addToBackStack(null)?.replace(R.id.forumFragment , addPost())?.commit()
                //val navControl = findNavController(R.id.forumFragment)
            }
            else{
             Toast.makeText(activity ,"Please login to make a query!" , Toast.LENGTH_SHORT).show()
            }

        }

        return view

        }

    private fun setUpRecyclerView() {

       // val recyclerView = view?.findViewById<RecyclerView>(R.id.recyclerViewForum)
        postDao = postDao()

        val postsCollection = postDao.postCollection
        val query = postsCollection.orderBy("createdAt" , Query.Direction.DESCENDING)
        val firestoreRecyclerOptions = FirestoreRecyclerOptions.Builder<Post>().setQuery(query , Post::class.java).build()

        myadapter = MyAdapter(firestoreRecyclerOptions)



        fragmentForumBinding.recyclerViewForum.adapter = myadapter
        fragmentForumBinding.recyclerViewForum.layoutManager = LinearLayoutManager(activity)

    }

    override fun onStart() {
        super.onStart()

        myadapter.startListening()
    }

    override fun onStop() {
        super.onStop()
        myadapter.stopListening()
    }
}

models.Post 类看起来像这样:

data class Post (val descrription: String = "",
                 val createdBy: User = User(),

                 val  currentTime:String = "",
                 val currentDate:String = "",
                 val createdAt:Long )

【问题讨论】:

  • 不清楚您使用的是 Cloud Firestore(默认)、Datatore 模式下的 Cloud Firestore 还是实时数据库,因为您可以互换使用这些术语。
  • 以后格式化代码块时,需要使用反引号字符`(通常在波浪号~键上)而不是撇号'。内联代码一个反引号,代码块三个反引号(在自己的行上)。

标签: android firebase kotlin google-cloud-firestore android-recyclerview


【解决方案1】:

为了让 Firebase SDK 将远程数据转换为所需的模型类,它需要有一个无参数的构造函数。

注意:我已将下面的descrription 更正为description

在您当前的models.Post 类中,它没有createdAt 的默认值:

data class Post (val description: String = "",
                 val createdBy: User = User(),
                 val currentTime: String = "",
                 val currentDate: String = "",
                 val createdAt: Long ) // <-- missing = 0L

应该是这样的:

data class Post (val description: String = "",
                 val createdBy: User = User(),
                 val currentTime: String = "",
                 val currentDate: String = "",
                 val createdAt: Long = 0L )

或者,您可以对models.Post 使用以下声明:

data class Post (val description: String,
                 val createdBy: User,
                 val currentTime: String,
                 val currentDate: String,
                 val createdAt: Long) {
  // empty constructor for Firebase with dummy data
  constructor(): this("", User(), "", "", 0L)
}

【讨论】:

    猜你喜欢
    • 2020-02-01
    • 2021-11-06
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2020-07-19
    • 2020-05-09
    相关资源
    最近更新 更多