Firebase数据未显示在模拟器中

时间:2019-09-30 22:30:24

标签: android-studio kotlin

未将Firebase数据导入并显示在模拟器中。 同步成功,构建成功,安装成功,但是只有likeimage和当前日期(不是Firebase数据库中的日期)和null(发布)显示

主要活动:

MainActivity类:AppCompatActivity(){

 var selectedCategory= FUNNY
lateinit var thoughtsAdapter: ThoughtsAdapter
val thoughts = arrayListOf<Thought>()
val thoughtsCollectionRef = FirebaseFirestore.getInstance().collection(THOUGHT_REF)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    fab.setOnClickListener {
        val addThoughtIntent = Intent(this, AddThoughtActivity::class.java)
        startActivity(addThoughtIntent)
    }
    thoughtsAdapter = ThoughtsAdapter(thoughts)
    thoughListView.adapter = thoughtsAdapter
    val layoutManager = LinearLayoutManager(this)
    thoughListView.layoutManager= layoutManager

    thoughtsCollectionRef
        .get()
        .addOnSuccessListener { snapshot ->
            for (document in snapshot.documents) {
                val data = document.data
                val name = data?.get("USERNAME") as? String
                val timestamp = data?.get("TIMESTAMP") as? Timestamp
                val thoughtText = data?.get("THOUGHT_TEXT") as? String
                val numberLikes = data?.get("NUMBER_LIKES") as? Long
                val numberComments = data?.get("NUMBER_COMMENTS") as? Long

                val newThought = Thought(
                    name, timestamp, thoughtText,
                    numberLikes?.toInt(), numberComments?.toInt()
                )
                thoughts.add(newThought)

            }

            thoughtsAdapter.notifyDataSetChanged()

        }.addOnFailureListener { exception: Exception ->
            Log.e("Exception", "Could not add post: Exception")
        }

}
    fun mainFunnyClicked(view: View) {
        if (selectedCategory == FUNNY) {
            mainFunnyButton.isChecked = true
            return
        }
        mainSeriouButton.isChecked = false
        mainCrazyButton.isChecked = false
        mainPopularButton.isChecked = false
        selectedCategory = FUNNY
    }

    fun mainSeriousClicked(view: View) {
        if (selectedCategory == SERIOUS) {
            mainSeriouButton.isChecked = true
            return
        }
        mainFunnyButton.isChecked = false
        mainCrazyButton.isChecked = false
        mainPopularButton.isChecked = false
        selectedCategory = SERIOUS
    }

    fun mainCrazyClicked(view: View) {
        if (selectedCategory == CRAZY) {
            mainCrazyButton.isChecked = true
            return
        }
        mainFunnyButton.isChecked = false
        mainSeriouButton.isChecked = false
        mainPopularButton.isChecked = false
        selectedCategory = CRAZY
    }

    fun mainPopularClicked(view: View) {
        if (selectedCategory == POPULAR) {
            mainPopularButton.isChecked = true
            return
        }
        mainFunnyButton.isChecked = false
        mainCrazyButton.isChecked = false
        mainSeriouButton.isChecked = false
        selectedCategory = POPULAR
    }

    }

思想活动:

class ThoughtsAdapter(val思想:ArrayList):RecyclerView.Adapter(){

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent?.context).inflate(R.layout.thought_list_view, parent, false)
    return ViewHolder(view)
}

override fun getItemCount(): Int {
    return thoughts.count()
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder?.bindThought(thoughts[position])
}

inner class ViewHolder (itemView: View) : RecyclerView.ViewHolder (itemView){

    val username = itemView?.findViewById<TextView>(R.id.ListViewUsername)
    val timestamp = itemView?.findViewById<TextView>(R.id.ListViewTimestamp)
    val thoughttext = itemView?.findViewById<TextView>(R.id.ListViewThoughtText)
    val numberlikes = itemView?.findViewById<TextView>(R.id.ListViewNumberLikesLabel)
    val likesimage = itemView?.findViewById<ImageView>(R.id.ListViewLikesImage)

    fun bindThought (thought: Thought) {
        username?.text = thought.username
        thoughttext?.text = thought.thoughtText
        numberlikes?.text = thought.numberLikes.toString()

        val dateFormatter = SimpleDateFormat("MMM d, h:mm a", Locale.getDefault ())
        val dateString = dateFormatter.format(Date ())
        timestamp?.text = dateString

    }
}

}

我希望显示用户名,发帖日期,想法/帖子(如图片,喜欢和评论)

仅显示当前日期,例如图片和显示为空

0 个答案:

没有答案
相关问题