0

I'm trying to set a Background color for alternate items in my ListView. I'm doing the following in my Custom ArrayAdapter class:

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

val currentExpense:Expense? = getItem(position)

itemExpenseListBinding = ExpensesRowDesignBinding.inflate(
    LayoutInflater.from(context),
    parent,
    false)

val itemView = itemExpenseListBinding.root


//        if(itemView == null){
//            itemView = convertView as LinearLayout
//        }


for ((index, item) in dataSource.withIndex()){

    if (index%2 == 0){

itemExpenseListBinding.linearExpensesRow.setBackgroundColor(Color.parseColor("#FEF8DD")

)
            }

        }

However, I'm getting end up with Background color being set on all my rows. I would like to have it set on just the even number rows in my ListView.

Update: Fixed this doing the following:

if (position%2 == 0){
                itemExpenseListBinding.linearExpensesRow.setBackgroundColor(Color.parseColor("#FEF8DD"))
            }

Can't believe I missed the position parameter lol.

Amey079
  • 131
  • 7

0 Answers0