I have a recyclerview that can have three different views base on the data with three different record types. If the data is composed of all type 1 records or both type 1 and type 2 records, then all of the data is displayed properly. But if a type 3 record is present, then no data following that type 3 record is displayed. And I am not getting any sort of error that I can find.
Any following type 1 and type 2 records are not displayed.
Here is the code for my recyclerview adapter:
class ShipsAdapter (private val stateroomInfo: ArrayList<StateroomAdapterData>,
private val listener: OnItemClickListener):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var rowType = 0
override fun getItemViewType(position: Int): Int {
rowType = when (stateroomInfo[position].adapterRecordType) {
Constants.SHIP_ROW -> Constants.SHIP_ROW
Constants.STATEROOM_ROW -> Constants.STATEROOM_ROW
else -> Constants.STATEROOM_PROPERTIES_ROW
}
return rowType
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
when (rowType){
Constants.SHIP_ROW -> {
val view = ListShipsRecyclerItemRowBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
ListShipsViewHolder(view)
}
Constants.STATEROOM_ROW -> {
val view = ListStateroomsRecyclerItemRowBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
ListStateroomsViewHolder(view)
}
else -> {
val view = ListStateroomPropertiesRecyclerItemRowBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
ListStateroomPropertiesViewHolder(view)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (rowType) {
Constants.SHIP_ROW -> (holder as ListShipsViewHolder).bind(position)
Constants.STATEROOM_ROW -> (holder as ListStateroomsViewHolder).bind(position)
Constants.STATEROOM_PROPERTIES_ROW -> (holder as ListStateroomPropertiesViewHolder).bind(position)
}
}
override fun getItemCount(): Int {
return stateroomInfo.size
}
inner class ListShipsViewHolder(shipsRow: ListShipsRecyclerItemRowBinding):
RecyclerView.ViewHolder(shipsRow.root),
View.OnClickListener {
private val tvShip = shipsRow.tvShip
private var ivEditShip = shipsRow.ivEditShip
private var ivDeleteShip = shipsRow.ivDeleteShip
private var ivStateroom = shipsRow.ivStateroom
init {
ivEditShip.setOnClickListener(this)
ivDeleteShip.setOnClickListener(this)
ivStateroom.setOnClickListener(this)
}
override fun onClick(v: View?) {
if (adapterPosition != RecyclerView.NO_POSITION)
if (v != null) {
when (v.id) {
R.id.ivEditShip -> listener.onItemClicked(adapterPosition, Constants.EDIT_SHIP_CLICKED)
R.id.ivDeleteShip -> listener.onItemClicked(adapterPosition, Constants.DELETE_SHIP_CLICKED)
R.id.ivStateroom -> listener.onItemClicked(adapterPosition, Constants.STATEROOM_CLICKED)
}
}
}
fun bind(position: Int) {
val shipData = stateroomInfo[position]
tvShip.text = shipData.ship
}
}
inner class ListStateroomsViewHolder(stateroomRow: ListStateroomsRecyclerItemRowBinding):
RecyclerView.ViewHolder(stateroomRow.root),
View.OnClickListener {
private val tvStateroom = stateroomRow.tvStateroom
private var ivEditStateroom = stateroomRow.ivEditStateroom
private var ivDeleteStateroom = stateroomRow.ivDeleteStateroom
init {
ivEditStateroom.setOnClickListener(this)
ivDeleteStateroom.setOnClickListener(this)
}
override fun onClick(v: View?) {
if (adapterPosition != RecyclerView.NO_POSITION)
if (v != null) {
when (v.id) {
R.id.ivEditStateroom -> listener.onItemClicked(adapterPosition, Constants.EDIT_STATEROOM_CLICKED)
R.id.ivDeleteStateroom -> listener.onItemClicked(adapterPosition, Constants.DELETE_STATEROOM_CLICKED)
}
}
}
fun bind(position: Int) {
val stateroomData = stateroomInfo[position]
tvStateroom.text = stateroomData.stateroom
}
}
inner class ListStateroomPropertiesViewHolder(stateroomPropertiesRow: ListStateroomPropertiesRecyclerItemRowBinding):
RecyclerView.ViewHolder(stateroomPropertiesRow.root),
View.OnClickListener {
private val tvBedrooms = stateroomPropertiesRow.tvBedrooms
private val tvBaths = stateroomPropertiesRow.tvBathrooms
private val tvConnecting = stateroomPropertiesRow.tvIsConnecting
private val ivEditStateroomProperties = stateroomPropertiesRow.ivEditStateroomProperties
init {
ivEditStateroomProperties.setOnClickListener(this)
}
fun bind(position: Int) {
val stateroomPropertiesData = stateroomInfo[position]
tvBedrooms.text = stateroomPropertiesData.numBedrooms
tvBaths.text = stateroomPropertiesData.numBaths
tvConnecting.text = stateroomPropertiesData.connectingSuite
}
override fun onClick(v: View?) {
if (adapterPosition != RecyclerView.NO_POSITION)
if (v != null) {
listener.onItemClicked(adapterPosition, Constants.EDIT_STATEROOM_PROPERTIES_CLICKED)
}
}
}
// this is required to communicate with the click listener in the Fragments
interface OnItemClickListener {
fun onItemClicked(position: Int, clickType: Int)
}
}
Related
I am trying to show multiple type of views in a recyclerview with a header and 5 rows followed by another header and 5 rows but only 4 rows are appearing.
Here is the log output from the adapter in the onBindViewHolder
Here is the code from the adapter
class DailyFragAdapter(
private val activityData: (DetailActivityData) -> Unit,
private val dates: List<Date>,
private val list : ArrayList<TabType1>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
/*
ListAdapter<TabType1, RecyclerView.ViewHolder>(Diff())*/ {
private lateinit var context: Context
private val HEADER = 1
private val ROW = 2
/*private class Diff : DiffUtil.ItemCallback<TabType1>() {
override fun areItemsTheSame(oldItem: TabType1, newItem: TabType1): Boolean {
return oldItem.header == newItem.header && oldItem.cps.cps[0].id == newItem.cps.cps[0].id
}
override fun areContentsTheSame(
oldItem: TabType1,
newItem: TabType1
): Boolean {
return oldItem.hashCode() == newItem.hashCode()
}
}*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
context = parent.context
val layoutInflater = LayoutInflater.from(context)
return if (viewType == 2) {
DVH(DailyFragRecyclerItemBinding.inflate(layoutInflater, parent, false))
} else {
TitleHolder(DailyTableHeaderBinding.inflate(layoutInflater, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val data: TabType1 = list[position]
if (holder is DVH) {
data.cps.cps.forEach {
Timber.i("is: a row")
holder.bind(it)
}
}else{
Timber.i("is: header")
holder as TitleHolder
holder.bind(data.header)
}
}
override fun getItemViewType(position: Int): Int {
val tabType1 = list[position]
return if (tabType1.header.title == "") {
ROW
} else {
HEADER
}
}
inner class TitleHolder(binding: DailyTableHeaderBinding) :
RecyclerView.ViewHolder(binding.root) {
private val groupHeader: TextView = binding.title
fun bind(title: Header) {
groupHeader.text = title.title
}
}
inner class DVH(binding: DailyFragRecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {
private var cpname: TextView = binding.cpName
private var day1: FrameLayout = binding.frameLayout
private var day2: FrameLayout = binding.frameLayout2
private var day3: FrameLayout = binding.frameLayout3
private var day4: FrameLayout = binding.frameLayout4
private var dayContainer: ArrayList<FrameLayout> = ArrayList()
fun bind(cpVo: CheckingPointVo) {
dayContainer.addAll(arrayListOf(day1, day2, day3, day4))
cpname.apply {
text = cpVo.name
setOnClickListener {
activityData.invoke(DetailActivityData(cpVo.id, cpVo.pcID))
}
}
val map = cpVo.chartEntriesMap
for (i in dates.indices) {
val dateID = BkpDateUtil.getDateId(dates[i])
Timber.i("dateID $dateID")
var ceVo: List<ChartEntryVo>? = map[dateID]
if (ceVo == null) {
ceVo = ArrayList<ChartEntryVo>()
ceVo.add(ChartEntryVo(0, dateID, 0L, 0L, "", false))
}
val entryValue = ceVo[0].value
when (cpVo.cpType) {
CheckingPointType.YES_NO -> {
val fl: FrameLayout = dayContainer[i]
fl.setOnClickListener {
openYesNoDialog(cpVo, ceVo[0])
}
if (entryValue == 1L) {
setIconInChartEntry(fl, R.drawable.ic_tick, cpVo)
} else {
setIconInChartEntry(fl, R.drawable.ic_no_entry, cpVo)
}
}
CheckingPointType.QUANTIFIABLE -> {
}
CheckingPointType.COUNTABLE -> {
}
CheckingPointType.CATEGORY -> {
}
CheckingPointType.TEXT -> {
}
}
}
dayContainer.clear()
}
}
private fun setIconInChartEntry(fl: FrameLayout, resID: Int, cpVo: CheckingPointVo) {
fl.getChildAt(0).visibility = GONE
val image: ImageView = fl.getChildAt(1) as ImageView
image.visibility = VISIBLE
image.setImageResource(resID)
/*image.setOnClickListener {
activityData.invoke(DetailActivityData(cpVo.id, cpVo.pcID))
}*/
}
private fun openYesNoDialog(cpVo: CheckingPointVo, ce: ChartEntryVo) {
val builder = AlertDialog.Builder(context)
val dataSourceArray = BkpUiUtil.getYesNoArray()
val date = getDateFromId(ce.dateKey)
val title: String = getDayText(date, 1) + " - " + cpVo.name
builder.setTitle(title)
builder.setSingleChoiceItems(
dataSourceArray, BkpUiUtil.getYesNoIndex(ce.value.toString())
) { dialog, which ->
ce.value = BkpUiUtil.getYesNoId(which)
activityData.invoke(
DetailActivityData(
cpvo = cpVo,
cevo = ce,updateChart = true
))
dialog.dismiss()
}
val d = builder.create()
d.show()
}
override fun getItemCount() = list.size
}
data class DetailActivityData(var cpID: Long = 0, var pcID: Long = 0, var cpvo:CheckingPointVo = CheckingPointVo(), var cevo:ChartEntryVo= ChartEntryVo(), var updateChart : Boolean = false)
data class TabType1(
val header: Header = Header(""), val cps: CheckingPointVoList = CheckingPointVoList(
cps = listOf(
CheckingPointVo()
)
)
)
This is how the adapter is given the data.
val data: ArrayList<TabType1> = arrayListOf(
TabType1(
header = Header("Group One")
),
TabType1(
cps = CheckingPointVoList(it)
),
TabType1(
header = Header("Group Two")
),
TabType1(
cps = CheckingPointVoList(it)
)
)
if(it1.updateChart){
vm.updateChartEntry(it1.cpvo,it1.cevo)
}else{
Intent(requireContext(), CpDetailActivity::class.java).apply {
putExtra("cprId", it1.cpID)
putExtra("pcId", it1.pcID)
requireActivity().startActivity(this)
}
}
}, arraylist,dayslist)
This is the output that I get.
What am I missing?
An Adapter should adapt ONE list of data, not multiple lists nested within others. Meaning you need an Adapter and RecyclerView for each list.
If you want to do that you need to add an Adapter/RecyclerView to DVH and display your CheckingPoint list in there.
look here ->
override fun getItemCount() = list.size
Its only going to bind 4 times because you only gave it 4 items.
And here you are just rebinding the same ViewHolder, you need to pass in the data to the ViewHolder and have it display the list of "cps"
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val data: TabType1 = list[position]
if (holder is DVH) {
holder.bind(data) //HERE
}else{
Timber.i("is: header")
holder as TitleHolder
holder.bind(data.header)
}
}
inner class DVH(binding: DailyFragRecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {
private var checkingPointVoRecyclerView: RecyclerView = binding.recyclerVew
private var checkingPointVoAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> = CheckingPointVoAdapter(dates)
checkingPointVoRecyclerView.adapter = checkingPointVoAdapter
fun bind(data: TabType1) {
checkingPointVoAdapter.list = data.cps
checkingPointVoAdapter.notifyDataSetChanged()
}
}
class CheckingPointVoAdapter(
private val dates: List<Date>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(){
public var list: List<CheckingPointVo> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
context = parent.context
val layoutInflater = LayoutInflater.from(context)
return ViewHolder(CheckingPointRecyclerItemBinding.inflate(layoutInflater, parent, false))
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val cpv: CheckingPointVo = list[position]
holder as ViewHolder
holder.bind(cpv)
}
inner class ViewHolder(binding: CheckingPointRecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {
private var cpname: TextView = binding.cpName
private var day1: FrameLayout = binding.frameLayout
private var day2: FrameLayout = binding.frameLayout2
private var day3: FrameLayout = binding.frameLayout3
private var day4: FrameLayout = binding.frameLayout4
private var dayContainer: ArrayList<FrameLayout> = ArrayList()
fun bind(cpVo: CheckingPointVo) {
dayContainer.addAll(arrayListOf(day1, day2, day3, day4))
cpname.apply {
text = cpVo.name
setOnClickListener {
activityData.invoke(DetailActivityData(cpVo.id, cpVo.pcID))
}
}
val map = cpVo.chartEntriesMap
for (i in dates.indices) {
val dateID = BkpDateUtil.getDateId(dates[i])
Timber.i("dateID $dateID")
var ceVo: List<ChartEntryVo>? = map[dateID]
if (ceVo == null) {
ceVo = ArrayList<ChartEntryVo>()
ceVo.add(ChartEntryVo(0, dateID, 0L, 0L, "", false))
}
val entryValue = ceVo[0].value
when (cpVo.cpType) {
CheckingPointType.YES_NO -> {
val fl: FrameLayout = dayContainer[i]
fl.setOnClickListener {
openYesNoDialog(cpVo, ceVo[0])
}
if (entryValue == 1L) {
setIconInChartEntry(fl, R.drawable.ic_tick, cpVo)
} else {
setIconInChartEntry(fl, R.drawable.ic_no_entry, cpVo)
}
}
CheckingPointType.QUANTIFIABLE -> {
}
CheckingPointType.COUNTABLE -> {
}
CheckingPointType.CATEGORY -> {
}
CheckingPointType.TEXT -> {
}
}
}
dayContainer.clear()
}
}
}
I did not test this but this is like 90% of what you need.
OnBindViewHolder gives you a view or you can imagine it as a row
, then you bind data to that row (setting text, etc..)
let's look at what you actually did in OnBindViewHolder
data.cps.cps.forEach { // you bind the data to the same row multiple times
Timber.i("is: a row")
holder.bind(it)
}
But there is only one row.
To solve this you need to pass a list of header items and row items only
sealed class DataItem {
abstract val id: Long
data class RowItem(val row: Row) : DataItem() {
override val id = row.id
}
data class HeaderItem(val header: Header) : DataItem() {
override val id = header.id
}
}
The Transform your row and header data to the DataItem Like this
val rowItems: List<DataItem> = rowList.map { DataItem.RowItem(it) }
val headertems: List<DataItem> = headerList.map { DataItem.BankItem(it) }
val items = rowItems+ headertems
//then pass the items to the adapter
So your adapter now will create a new ViewHoler to each item in the list and you can check for it as
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ITEM_VIEW_TYPE_ITEM_ROW -> DVH(DailyFragRecyclerItemBinding.inflate(layoutInflater, parent, false))
ITEM_VIEW_TYPE_ITEM_HEADER -> TitleHolder(DailyTableHeaderBinding.inflate(layoutInflater, parent, false))
else -> throw ClassCastException("Unknown viewType $viewType")
}
}
then bind data to each view as
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is DVH-> {
val item = getItem(position) as DataItem.RowItem
holder.bind(item)
}
is TitleHolder -> holder.bind(item)
}
}
Finally you need to adjust getItemViewType
override fun getItemViewType(position: Int): Int {
return when (getItem(position)) {
is DataItem.RowItem -> ITEM_VIEW_TYPE_ITEM_ROW
is DataItem.HeaderItem -> ITEM_VIEW_TYPE_ITEM_HEADER
}
}
Recently, I was faced with an issue that is a bit hard for me to solve
I need to put 2 models in the List adapter, but it always says 2 types of arguments expected
Here is the Link of the whole project
and I want to have Transaction and Bank models in ListAdapter
you can read the project README.
The adapter that I want to have 2 models is in ui.TransactionHistory
here is my whole adapter class:
class TransactionHistory() :
ListAdapter<Transaction, RecyclerView.ViewHolder>(BillDiffCallback()) {
private val ITEM_VIEW_TYPE_EMPTY = 0
private val ITEM_VIEW_TYPE_ITEM = 1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ITEM_VIEW_TYPE_ITEM -> ViewHolder.from(parent)
ITEM_VIEW_TYPE_EMPTY -> EmptyViewHolder.from(parent)
else -> throw ClassCastException("Unknown viewType $viewType")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is ViewHolder -> {
val item = getItem(position)
holder.bind(item, clickListener)
// holder.bind2(Bank, clickListener)
}
is EmptyViewHolder -> {
holder.bind()
}
}
}
lateinit var clickListener: AdapterListener2
fun setOnclickListener(listener: AdapterListener2) {
clickListener = listener
}
override fun getItemViewType(position: Int): Int {
return if (itemCount > 0)
ITEM_VIEW_TYPE_ITEM
else
ITEM_VIEW_TYPE_EMPTY
}
class ViewHolder
private constructor(val binding: ItemUserTransactionListBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: Transaction, adapterListener2: AdapterListener2) {
binding.transaction = item
binding.clickListener = adapterListener2
binding.executePendingBindings()
if (item.type == "payPayment") {
binding.transactionStatus.text = "برداخت قسط"
} else if (item.type == "decrease") {
binding.transactionStatus.text = "برداشت"
} else if (item.type == "increase") {
binding.transactionStatus.text = "واریز"
}
if (item.decrease == null) {
binding.amount.text = item.increase
} else {
binding.amount.text = item.decrease
}
}
fun bind2(item2: Bank, adapterListener2: AdapterListener2) {
binding.bankInfo = item2
binding.clickListener = adapterListener2
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemUserTransactionListBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}
}
}
}
class EmptyViewHolder private constructor(val binding: ItemUserTransactionListBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind() {
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): EmptyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemUserTransactionListBinding.inflate(layoutInflater, parent, false)
return EmptyViewHolder(binding)
}
}
}
class BillDiffCallback : DiffUtil.ItemCallback<Transaction>() {
override fun areItemsTheSame(oldItem: Transaction, newItem: Transaction): Boolean {
return oldItem.transId == newItem.transId
}
override fun areContentsTheSame(
oldItem: Transaction,
newItem: Transaction
): Boolean {
return oldItem == newItem
}
}
class AdapterListener2(
val clickListener: (id: Long?) -> Unit,
val deleteListener: (category: Transaction) -> Unit
) {
fun onclick(transaction: Transaction) = clickListener(transaction.userId)
fun onDeleteClick(userInfo: Transaction) = deleteListener(userInfo)
}
whenever i placed secound model here:
ListAdapter<Transaction, **Bank**, RecyclerView.ViewHolder>(BillDiffCallback()) {}
it says 2 type argument expected.
I don't know if it helps you or not but some one told me I have to use join in Kotlin
THANKS FOR YOUR ANSWERS :)
ListAdaper can only accept one data model however you can add multiple items using another class like Sealed Class
sealed class DataItem {
abstract val id: Long
data class TransactionItem(val transaction: Transaction): DataItem() {
override val id = transaction.transId
}
object Empty: DataItem() {
override val id = Long.MIN_VALUE
}
}
and deal with this only class in your ListAdapter for your code you will need to apply this changes
class TransactionHistory() :
ListAdapter<DataItem, RecyclerView.ViewHolder>(BillDiffCallback()) {
private val ITEM_VIEW_TYPE_EMPTY = 0
private val ITEM_VIEW_TYPE_ITEM_TRANSACTION = 1
private val ITEM_VIEW_TYPE_ITEM_BANK = 2
private val adapterScope = CoroutineScope(Dispatchers.Default)
/**
* DO NOT USE .submit(), use the method bellow
*/
fun addTransactionsAndBanks(transactionList: List<Transaction>?, bankList: List<Bank>?) {
adapterScope.launch {
val transactionItems: List<DataItem> = when {
transactionList == null || transactionList.isEmpty() -> listOf(DataItem.Empty)
else -> transactionList.map { DataItem.TransactionItem(it) }
}
val bankItems: List<DataItem> = when {
bankList == null || bankList.isEmpty() -> listOf(DataItem.Empty)
else -> bankList.map { DataItem.BankItem(it) }
}
val items = transactionItems + bankItems
withContext(Dispatchers.Main) {
submitList(items)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ITEM_VIEW_TYPE_ITEM_TRANSACTION -> ViewHolder.from(parent)
ITEM_VIEW_TYPE_ITEM_BANK -> ViewHolder.from(parent)
ITEM_VIEW_TYPE_EMPTY -> EmptyViewHolder.from(parent)
else -> throw ClassCastException("Unknown viewType $viewType")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is ViewHolder -> {
when (val item = getItem(position)) {
is DataItem.TransactionItem -> holder.bind(item.transaction, clickListener)
is DataItem.BankItem -> holder.bind2(item.bank, clickListener)
}
}
is EmptyViewHolder -> holder.bind()
}
}
lateinit var clickListener: AdapterListener2
fun setOnclickListener(listener: AdapterListener2) {
clickListener = listener
}
override fun getItemViewType(position: Int): Int {
return when (getItem(position)) {
is DataItem.Empty -> ITEM_VIEW_TYPE_EMPTY
is DataItem.TransactionItem -> ITEM_VIEW_TYPE_ITEM_TRANSACTION
is DataItem.BankItem -> ITEM_VIEW_TYPE_ITEM_BANK
}
}
class ViewHolder
private constructor(val binding: ItemUserTransactionListBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: Transaction, adapterListener2: AdapterListener2) {
binding.transaction = item
binding.clickListener = adapterListener2
binding.executePendingBindings()
if (item.type == "payPayment") {
binding.transactionStatus.text = "برداخت قسط"
} else if (item.type == "decrease") {
binding.transactionStatus.text = "برداشت"
} else if (item.type == "increase") {
binding.transactionStatus.text = "واریز"
}
if (item.decrease == null) {
binding.amount.text = item.increase
} else {
binding.amount.text = item.decrease
}
}
fun bind2(item2: Bank, adapterListener2: AdapterListener2) {
binding.bankInfo = item2
binding.clickListener = adapterListener2
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemUserTransactionListBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}
}
}
}
class EmptyViewHolder private constructor(val binding: ItemUserTransactionListBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind() {
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): EmptyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemUserTransactionListBinding.inflate(layoutInflater, parent, false)
return EmptyViewHolder(binding)
}
}
}
class BillDiffCallback : DiffUtil.ItemCallback<DataItem>() {
override fun areItemsTheSame(oldItem: DataItem, newItem: DataItem): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: DataItem, newItem: DataItem): Boolean {
return oldItem == newItem
}
}
class AdapterListener2(
val clickListener: (id: Long?) -> Unit,
val deleteListener: (category: Transaction) -> Unit
) {
fun onclick(transaction: Transaction) = clickListener(transaction.userId)
fun onDeleteClick(userInfo: Transaction) = deleteListener(userInfo)
}
sealed class DataItem {
abstract val id: Long
data class TransactionItem(val transaction: Transaction) : DataItem() {
override val id = transaction.transId
}
data class BankItem(val bank: Bank) : DataItem() {
override val id = bank.bankId
}
object Empty : DataItem() {
override val id = Long.MIN_VALUE
}
}
You can build the list based on only a single data source. If you want to have multiple data sources, you should create a third data model and add the other two models in it.
data class ListData(val transaction : Transaction , val Bank : Bank)
ListView/Recycler view will create number of list items based on the list of objects passed.
ListAdapter<ListData, RecyclerView.ViewHolder>
I found the answer to my Question
We have to use Join in query:
//for single info
#Query("SELECT `transaction`.increase, `transaction`.decrease, bank.bank_name From `transaction` JOIN bank WHERE `transaction`.bank_id=:key ")
fun joinTables(key: Long): LiveData<TransactionAndBank>?
//for list of info
#Query("SELECT `transaction`.increase, `transaction`.decrease,`transaction`.type, bank.bank_name From `transaction` JOIN bank WHERE `transaction`.bank_id=:key ")
fun joinAllTables(key: Long): LiveData<List<TransactionAndBank>>
I had to place it In TransactionDAO
source
There are two Recyclerview in same activity rv1 and rv2. Selected item of rv1 will show in rv2
rv1:
Here I select multiple items
rv2:
it should shows like this
Adapter class of rv1:
class ServiceJobAdapter(val context: Context, var list:ArrayList<JobRespo>,var listener:OnItemClick):
RecyclerView.Adapter<ServiceJobAdapter.ItemsCarrier>(){
var currentSelectedIndex= -1
var selectedItem = SparseBooleanArray()
var animationItemsIndex = SparseBooleanArray()
private val reverseAllAnimations = false
var holder:ItemsCarrier? =null
class ItemsCarrier(itemView: View) : RecyclerView.ViewHolder(itemView)
{
var jobName = itemView.findViewById<TextView>(R.id.job_name)
var jobPrice = itemView.findViewById<TextView>(R.id.job_price)
var iconBack = itemView.findViewById(R.id.icon_back) as RelativeLayout
var iconFront = itemView.findViewById(R.id.icon_front) as RelativeLayout
var iconContainer = itemView.findViewById(R.id.icon_container) as CardView
fun binde(jobRespo: JobRespo) {
jobName.text = jobRespo.repDesc
jobPrice.text="₹"+jobRespo.repRice
if (iconFront.visibility == View.VISIBLE)
{
jobRespo.setChecked(false)
}else{
jobRespo.setChecked(true)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemsCarrier {
val view = LayoutInflater.from(context).inflate(R.layout.job_row,parent,false)
return ItemsCarrier(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ItemsCarrier, position: Int) {
this.holder = holder
holder.itemView.isActivated = selectedItem[position,false]
holder.binde(list[position])
applyIconAnimation(holder, position)
// apply click events
applyClickEvents(holder, position)
}
private fun applyClickEvents(holder: ItemsCarrier, position: Int) {
holder.iconContainer.setOnClickListener {
listener.onIconClicked(position)
listener.AllItem(list[position]) }
}
private fun applyIconAnimation(holder: ItemsCarrier, position: Int)
{
if (selectedItem.get(position, false)) {
holder.iconFront!!.visibility = View.GONE
resetIconYAxis(holder.iconBack)
holder.iconBack.visibility = View.VISIBLE
holder.iconBack.alpha = 1f
if (currentSelectedIndex == position) {
FlipAnimator.flipView(context, holder.iconBack, holder.iconFront, true)
resetCurrentIndex()
}
} else {
holder.iconBack.visibility = View.GONE
resetIconYAxis(holder.iconFront)
holder.iconFront.visibility = View.VISIBLE
holder.iconFront.alpha = 1f
if (reverseAllAnimations && animationItemsIndex.get(
position,
false
) || currentSelectedIndex == position
) {
FlipAnimator.flipView(context, holder.iconBack, holder.iconFront, false)
resetCurrentIndex() } } }
private fun resetIconYAxis(view: RelativeLayout?)
{
if (view!!.rotationY != 0f) {
view.rotationY = 0f
}
}
private fun resetCurrentIndex() {
currentSelectedIndex = -1
}
fun toggleSelection(pos: Int) {
currentSelectedIndex = pos
if (selectedItem.get(pos, false)) {
selectedItem.delete(pos)
animationItemsIndex.delete(pos)
} else {
selectedItem.put(pos, true)
animationItemsIndex.put(pos, true)
}
notifyItemChanged(pos)
}
fun getSelectedItems(): ArrayList<JobRespo>
{
val selectItems = ArrayList<JobRespo>()
for(item in list)
{
if (!item.isChecked())
{
selectItems.add(item)
}
}
return selectItems}}
Using Interface
interface OnItemClick{
fun onIconClicked(position: Int)
fun AllItem(jobs:JobRespo)}
Implement interface in Activity
override fun onIconClicked(position: Int) {
mAdapter!!.toggleSelection(position)
}
override fun AllItem(jobs: JobRespo) {
val selectedItems:ArrayList<JobRespo>
=mAdapter!!.getSelectedItems()
selectIte.addAll(selectedItems)
}
Adapter class of 2nd Recyclerview:
lass SelectItemAdapter(val context: Context, var list:List<JobRespo>): RecyclerView.Adapter<SelectItemAdapter.ItemsCarriers>(){
class ItemsCarriers(itemView: View) : RecyclerView.ViewHolder(itemView) {
var itemName = itemView.findViewById<TextView>(R.id.item_name)
var itemPrice= itemView.findViewById<TextView>(R.id.item_peice)
var itmDelet =itemView.findViewById<LinearLayout>(R.id.item_delete)
fun bind(job: JobRespo) {
itemName.text = job.repDesc
itemPrice.text = job.repRice }
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemsCarriers {
val root = LayoutInflater.from(context).inflate(R.layout.select_item_row,parent,false)
return ItemsCarriers(root)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ItemsCarriers, position: Int) {
holder.bind(list[position])}}
Now how can i pass multiselected data from one recyclerview to anathor recyclerview of same activity?
Thanks your consideration and guidded to me :)
You can create a method in a second adapter to update the selected value like below and use it when you getting selected items.
fun setSelectedData(data:ArrayList<JobRespo>){
this.list.clear()
this.list.addAll(data)
notifyDataSetChanged ();
}
Why doesn't my function allow me to set a content description for my ImageView? It seems to allow me to set a drawable resource and text though.
class MyAdapter(
private val dataIVPlayOrPause: Drawable,
private val dataTVSong: String
) : RecyclerView.Adapter<RVAdapterEmbark.MyViewHolder>() {
private val typeHeader = 1
private val typeItem = 2
override fun onCreateViewHolder(parent: ViewGroup, type: Int): MyViewHolder {
return when (type) {
typeHeader -> MyViewHolder(inflateHelper(R.layout.header, parent))
typeItem -> MyViewHolder(inflateHelper(R.layout.body, parent))
else -> MyViewHolder(inflateHelper(R.layout.body, parent))
}
}
override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
if (getItemViewType(position) == typeHeader) {
} else if (getItemViewType(position) == typeItem) {
val ivPlayPause = viewHolder.itemView. iv_Play_Pause
ivPlayPause(R.drawable.play)
ivPlayPause = R.string.play.toString()
val buttonClickListener = View.OnClickListener {
if (tvSongName.text == null) {
playSong(tvSongName, ivPlayPause)
} else {
pauseSong(tvSongName, ivPlayPause)
}
}
}
}
private fun inflateHelper(resId: Int, parent: ViewGroup): View {
return LayoutInflater.from(parent.context).inflate(resId, parent, false)
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun getItemCount(): Int {
return dataTitle.size + 1
}
override fun getItemViewType(position: Int): Int {
return if (position == 0) typeHeader
else typeItem
}
private fun playSong(
tvSongName: TextView,
ivPlayPause: ImageView
) {
tvSongName.text = "Song name"
ivPlayPause.setImageResource(R.drawable.pause)
ivPlayPause.contentDescription = getString(R.string.pause)
}
}
Since you are in a RecyclerView.Adapter, you can't access the Activity, Fragment or Context instance in which getString() is declared.
In this case you can use the Context of your ImageView to get the string:
ivPlayPause.contentDescription = ivPlayPause.context.getString(R.string.pause)
I've got a Popup menu that should launch a Maps intent whenever a Popup menu item is clicked. In popupMenu.setOnMenuItemClickListener, does anyone know how I can pass the String of the clicked Popup menu item (from the arrayAMap array) and use it for an intent? I've already got the Arrays but I can't seem to figure out that correct way to implement this function.
class MyAdapter(
private val mCtx: Context,
var myList: MutableList<ItemRV>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), PopupMenu.OnMenuItemClickListener {
private var mClickListener: ItemClickListener? = null
lateinit var mAdView : AdView
private val itemRV = 1
private val itemAD = 2
override fun getItemViewType(position: Int): Int {
return if (position % 4 == 0) {
itemAD
} else {
itemRV
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == itemAD) {
val v = LayoutInflater.from(mCtx).inflate(R.layout.item_ad, parent, false)
AdViewHolder(v)
} else {
val v = LayoutInflater.from(mCtx).inflate(R.layout.item_rv, parent, false)
AreaViewHolder(v)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder) {
is AdViewHolder -> {
MobileAds.initialize(mCtx) {}
mAdView = holder.itemView.findViewById(R.id.adView)
val adRequest = AdRequest.Builder().build()
mAdView.loadAd(adRequest)
}
is AreaViewHolder -> {
val positionToBind = position - position / 4 - 1
val product = myList[positionToBind]
holder.tvTitle.text = product.itemTitle
}
}
}
override fun getItemCount(): Int {
return myList.size
}
inner class AdViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView), View.OnClickListener {
override fun onClick(v: View?) {
}
}
inner class AreaViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView), View.OnClickListener {
var tvTitle: TextView = itemView.tvtitle
// Use package name which we want to check
private val isAppInstalled = appInstalledOrNot("com.google.android.apps.maps")
private val isLiteAppInstalled = appInstalledOrNot("com.google.android.apps.mapslite")
fun launchMapIntent(nameLocation: String) {
val mapPkg = when {
isAppInstalled -> "com.google.android.apps.maps"
isLiteAppInstalled -> "com.google.android.apps.mapslite"
else -> null
}
val mapIntent = if(mapPkg != null) {
val gmmIntentUri = Uri.parse("geo:0,0?q=$nameLocation")
Intent(Intent.ACTION_VIEW, gmmIntentUri).setPackage(mapPkg)
} else {
val encLoc = Uri.encode(nameLocation)
val str = "https://www.google.com/maps/place/$encLoc/"
Intent(Intent.ACTION_VIEW, Uri.parse(str))
}
mCtx.startActivity(mapIntent)
}
val arrayA = arrayOf(view.resources.getString(R.string.stockholm),
view.resources.getString(R.string.copenhagen))
val arrayAMap = arrayOf("Stockholm, Sweden", "Copenhagen, Denmark")
fun launchPopupMenu(namePopupItemLocation: Array<String>, nameLocation: Array<String>){
val popupMenu = PopupMenu(ibMap.context, ibMap)
for (item in namePopupItemLocation) {
popupMenu.menu.add(item)
}
popupMenu.setOnMenuItemClickListener {
launchMapIntent(nameLocation.get())
true
}
popupMenu.show()
}
ibMap.setOnClickListener {
when(tvTitle.text.toString()) {
"A" -> launchMapIntent("Paris, France")
"B" -> launchPopupMenu(arrayA, arrayAMap)
else -> return#setOnClickListener
}
}
}
private fun appInstalledOrNot(uri: String): Boolean {
val pm = mCtx.packageManager
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES)
return true
} catch (e: PackageManager.NameNotFoundException) {
}
return false
}
}
// Parent activity will implement this method to respond to click events
interface ItemClickListener {
fun onItemClick(view: View, position: Int)
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
}
}
You can get clicked menu item in setOnMenuItemClickListener and from that menu item you can get title of that menu
popupMenu.setOnMenuItemClickListener { item ->
nameLocation.forEach {
if (it.toLowerCase().startsWith(item.title.toLowerCase())) {
launchMapIntent(it)
}
}
true
}
Hope this will help!!