Issue
I am creating social media app on that I have done with Retrieveing database to recycler view and Onclick listener also but I want to initialize layout inflater to adapter class for bottom sheet. I am using adapter class for clicking events and playing videos like that. I can't add layout inflater to adapter class. Because I am just inflating XML using adapter and adapter class Contains recycler view class only it's not contains Appcompat Activity. So think this a problem and why I am asking to retrieve firebase database to recycler view without adapter.
@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: learning English
EDIT: I Added code of adapter class.
Thanks in advance.
Solution
A LayoutInflater
is just an object. It is easy to get one, such as by calling getLayoutInflater()
on your Activity
. And you can then pass it to the RecyclerView.Adapter
, such as via a constructor parameter.
For example, here is a RecyclerView.Adapter
that accepts a LayoutInflater
as a constructor parameter:
/*
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)
}
}
}
The activity can create an instance of it via 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() }
}
Answered By - CommonsWare
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)