如何在没有适配器的情况下将 firebase 数据库检索到 recyclerview?
How to retrieve firebase database to recylerview without adapter?
我正在创建社交媒体应用程序,我已经完成了将数据库检索到回收站视图和 Onclick 侦听器,但我想将布局 inflater 初始化为底部 class 的适配器 sheet。
我正在使用适配器 class 来点击事件和播放类似的视频。我无法将布局充气器添加到适配器 class。
因为我只是在使用适配器和适配器 class 充气 XML
包含回收器视图 class 只是它不包含 Appcompat Activity。所以认为这是一个问题,以及为什么我要求在没有适配器的情况下将 firebase 数据库检索到回收站视图。
@Suppress("DEPRECATION")
class CommentAdapter(private var context: Context, private var data:
List<Comment>) : RecyclerView.Adapter<CommentAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val row: View = LayoutInflater.from(context).inflate(R.layout.commentpostsnamashivaya, parent, false)
return MyViewHolder(row)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.commentTv.text = data[position].getComment()
}
override fun getItemCount(): Int {
return data.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var commentTv: TextView = itemView.findViewById(R.id.comment_text)
var report_post_of_lord: ImageView = itemView.findViewById(R.id.report_user_of_lordshivaproduct)
var sharepost: ImageView = itemView.findViewById(R.id.post_share_somam)
init {
itemView.setOnClickListener {
val intent = Intent(context, viewing_post_nama::class.java)
val position: Int = adapterPosition
intent.putExtra("Comment", data[position].getComment())
context.startActivity(intent)
}
report_post_of_lord.setOnClickListener{
Toast.makeText(context,"Reporting Post . . . . .", Toast.LENGTH_LONG).show()
//here is my problem here i just only mentioned layoutinflater only.
val layinf =
LayoutInflater.from(context).inflate(R.layout.thisisforbsheet, parent,
false)
}
sharepost.setOnClickListener{
val shareIntent = Intent(Intent.ACTION_SEND)
val text = data[position].getComment() + "BY COMMENTING APP FROM LORD SHIVA PRODUCTS FAMILY :) "
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
context.startActivity(Intent.createChooser(shareIntent,"SHARE COMMENT"))
}
}
}
Ps: 学习英语
编辑:
我添加了适配器代码 class.
提前致谢。
是的,我认为它可以解决您的问题。您可以使用 firebase 回收器适配器在 recyclerview 中填充数据。这是 link 我们如何使用它
https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/
一个LayoutInflater
只是一个对象。很容易得到一个,例如通过在 Activity
上调用 getLayoutInflater()
。然后您可以将其传递给 RecyclerView.Adapter
,例如通过构造函数参数。
例如,这里是一个接受 LayoutInflater
作为构造函数参数的 RecyclerView.Adapter
:
/*
Copyright (c) 2018-2020 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Elements of Android Jetpack_
https://commonsware.com/Jetpack
*/
package com.commonsware.jetpack.sampler.recyclerview
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.commonsware.jetpack.sampler.recyclerview.databinding.RowBinding
class ColorAdapter(private val inflater: LayoutInflater) :
ListAdapter<Int, ColorViewHolder>(ColorDiffer) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ColorViewHolder {
return ColorViewHolder(RowBinding.inflate(inflater, parent, false))
}
override fun onBindViewHolder(holder: ColorViewHolder, position: Int) {
holder.bindTo(getItem(position))
}
private object ColorDiffer : DiffUtil.ItemCallback<Int>() {
override fun areItemsTheSame(oldColor: Int, newColor: Int): Boolean {
return oldColor == newColor
}
override fun areContentsTheSame(oldColor: Int, newColor: Int): Boolean {
return areItemsTheSame(oldColor, newColor)
}
}
}
activity 可以通过 ColorAdapter(layoutInflater)
创建它的实例:
/*
Copyright (c) 2018-2020 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Elements of Android Jetpack_
https://commonsware.com/Jetpack
*/
package com.commonsware.jetpack.sampler.recyclerview
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.commonsware.jetpack.sampler.recyclerview.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity() {
private val random = Random()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.items.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
addItemDecoration(
DividerItemDecoration(this@MainActivity, DividerItemDecoration.VERTICAL)
)
adapter = ColorAdapter(layoutInflater).apply {
submitList(buildItems())
}
}
}
private fun buildItems() = List(25) { random.nextInt() }
}
我正在创建社交媒体应用程序,我已经完成了将数据库检索到回收站视图和 Onclick 侦听器,但我想将布局 inflater 初始化为底部 class 的适配器 sheet。 我正在使用适配器 class 来点击事件和播放类似的视频。我无法将布局充气器添加到适配器 class。 因为我只是在使用适配器和适配器 class 充气 XML 包含回收器视图 class 只是它不包含 Appcompat Activity。所以认为这是一个问题,以及为什么我要求在没有适配器的情况下将 firebase 数据库检索到回收站视图。
@Suppress("DEPRECATION")
class CommentAdapter(private var context: Context, private var data:
List<Comment>) : RecyclerView.Adapter<CommentAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val row: View = LayoutInflater.from(context).inflate(R.layout.commentpostsnamashivaya, parent, false)
return MyViewHolder(row)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.commentTv.text = data[position].getComment()
}
override fun getItemCount(): Int {
return data.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var commentTv: TextView = itemView.findViewById(R.id.comment_text)
var report_post_of_lord: ImageView = itemView.findViewById(R.id.report_user_of_lordshivaproduct)
var sharepost: ImageView = itemView.findViewById(R.id.post_share_somam)
init {
itemView.setOnClickListener {
val intent = Intent(context, viewing_post_nama::class.java)
val position: Int = adapterPosition
intent.putExtra("Comment", data[position].getComment())
context.startActivity(intent)
}
report_post_of_lord.setOnClickListener{
Toast.makeText(context,"Reporting Post . . . . .", Toast.LENGTH_LONG).show()
//here is my problem here i just only mentioned layoutinflater only.
val layinf =
LayoutInflater.from(context).inflate(R.layout.thisisforbsheet, parent,
false)
}
sharepost.setOnClickListener{
val shareIntent = Intent(Intent.ACTION_SEND)
val text = data[position].getComment() + "BY COMMENTING APP FROM LORD SHIVA PRODUCTS FAMILY :) "
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
context.startActivity(Intent.createChooser(shareIntent,"SHARE COMMENT"))
}
}
}
Ps: 学习英语
编辑: 我添加了适配器代码 class.
提前致谢。
是的,我认为它可以解决您的问题。您可以使用 firebase 回收器适配器在 recyclerview 中填充数据。这是 link 我们如何使用它
https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/
一个LayoutInflater
只是一个对象。很容易得到一个,例如通过在 Activity
上调用 getLayoutInflater()
。然后您可以将其传递给 RecyclerView.Adapter
,例如通过构造函数参数。
例如,这里是一个接受 LayoutInflater
作为构造函数参数的 RecyclerView.Adapter
:
/*
Copyright (c) 2018-2020 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Elements of Android Jetpack_
https://commonsware.com/Jetpack
*/
package com.commonsware.jetpack.sampler.recyclerview
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.commonsware.jetpack.sampler.recyclerview.databinding.RowBinding
class ColorAdapter(private val inflater: LayoutInflater) :
ListAdapter<Int, ColorViewHolder>(ColorDiffer) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ColorViewHolder {
return ColorViewHolder(RowBinding.inflate(inflater, parent, false))
}
override fun onBindViewHolder(holder: ColorViewHolder, position: Int) {
holder.bindTo(getItem(position))
}
private object ColorDiffer : DiffUtil.ItemCallback<Int>() {
override fun areItemsTheSame(oldColor: Int, newColor: Int): Boolean {
return oldColor == newColor
}
override fun areContentsTheSame(oldColor: Int, newColor: Int): Boolean {
return areItemsTheSame(oldColor, newColor)
}
}
}
activity 可以通过 ColorAdapter(layoutInflater)
创建它的实例:
/*
Copyright (c) 2018-2020 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Elements of Android Jetpack_
https://commonsware.com/Jetpack
*/
package com.commonsware.jetpack.sampler.recyclerview
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.commonsware.jetpack.sampler.recyclerview.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity() {
private val random = Random()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.items.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
addItemDecoration(
DividerItemDecoration(this@MainActivity, DividerItemDecoration.VERTICAL)
)
adapter = ColorAdapter(layoutInflater).apply {
submitList(buildItems())
}
}
}
private fun buildItems() = List(25) { random.nextInt() }
}