Himanshu Bansal
Himanshu Bansal

Reputation: 7

Trying to fetch Firebase Cloud Database data and show it in RecyclerView in a fragment

I am trying to fetch my data in a RecyclerView from Firebase Cloud Datastore. But when I do, I get the following error:

java.lang.RuntimeException: Could not deserialize object. Class models.Post does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped

This is the fragment which I am using:

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()
    }
}

and the models.Post class looks like this:

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

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

Upvotes: 0

Views: 50

Answers (1)

samthecodingman
samthecodingman

Reputation: 26171

For the Firebase SDK to convert the remote data to the desired model class, it needs to have a no-argument constructor.

Note: I've corrected descrription to description below.

In your current models.Post class, it doesn't have a default value for createdAt:

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

So it should be:

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

Alternatively, you can have the following declaration for 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)
}

Upvotes: 1

Related Questions