0

I'm having a RecyclerView, inside its layout there's a view "tvDateGrouped", I want it to be displayed only when the current position date and previous position date are not same. Below is the code I tried, but it isn't working

This Code is inside the adapter class, inside the onBind() method

  if (position != 0 && transactions[position].date == 
    transactions[holder.adapterPosition.minus(1)].date) {
                holder.view.tvDateGrouped.visibility = View.GONE
            } else {
                holder.view.tvDateGrouped.text = formattedDateString
            }

and if I change position != 1 , the app crashes

below is the code when app crashes

  if (position != 1 && transactions[position].date == 
        transactions[holder.adapterPosition.minus(1)].date) {
                    holder.view.tvDateGrouped.visibility = View.GONE
                } else {
                    holder.view.tvDateGrouped.text = formattedDateString
                }

Edit: I've changed the code as the below, but the app is crashing now-

if ((transactions[position].date == transactions[position - 1].date) && 
(position > 0)) {
    holder.view.tvDateGrouped.visibility = View.GONE
} else {
    holder.view.tvDateGrouped.visibility = View.VISIBLE
    holder.view.tvDateGrouped.text = formattedDateString
}

Please Help me resolve this issue

  • `0 - 1 = -1`, an invalid index. In the first piece of code you put a safeguard against that, but not in the second one. – Nicolas Jun 04 '20 at 17:02

1 Answers1

0

Your crash is simply happening when position is 0 then position - 1 will be -1 which is an invalid index and you will get a runtime exception.

But why the first solution isn't working? One clear reason is because you've forgotten to make the text visible again. So change it to:

 if (position > 0 && transactions[position].date == transactions[position - 1].date) {
    holder.view.tvDateGrouped.visibility = View.GONE
} else {
    holder.view.tvDateGrouped.visibility = View.VISIBLE
    holder.view.tvDateGrouped.text = formattedDateString
}
momvart
  • 1,737
  • 1
  • 20
  • 32
  • Hi @Mohammad, Thank you, the code worked but not exactly as I wanted it to. It seems like everytime else block is getting executed, so to make sure everything is working fine I changed the if statement as if (transactions[position].date == transactions[position - 1].date && position > 0) and now, the app is crashing, seems like there is some error in transactions[position].date == transactions[position - 1].date line – Anmol Singh Sahi Jun 04 '20 at 17:36
  • the solution worked but not exactly, please check the above comment of mine – Anmol Singh Sahi Jun 04 '20 at 17:37
  • @AnmolSinghSahi What do you mean but not exactly? What behavior do you expect and not see? About the crash: the order of booleans is important. It's a rule that if the left operand of an and operation is false, the right one is not evaluated. So, when you move `position > 0` to the right, the previous part is run for zero and crash! – momvart Jun 04 '20 at 21:01
  • [link] (https://imageshack.com/i/pnQFv4tWp), please check this link, "tvDateGrouped" is the top gray colored rounded corner info tab, and it is showing everytime, but I want it to show only when the date is different from the previous date, i.e. I want to show it exactly just like the chats app like(WhatsApp) – Anmol Singh Sahi Jun 05 '20 at 06:36
  • @AnmolSinghSahi Check that if your `date` field contains time. I guess it contains time and equality never happens. If true, do the equality check just on the date part. – momvart Jun 05 '20 at 07:01