Home > Software engineering >  How to properly get the ID of the fragment the button is assigned to
How to properly get the ID of the fragment the button is assigned to

Time:05-01

btn.setOnCheckedChangeListener { _, isChecked ->
        if(isChecked){
            //instance of a list
             spotsProvider.spotsList?.find { spots.ID.equals(spots.ID)}?.likes = spots.likes   1
            numLikes.text = spots.likes?.toString()

            //refreshFragment(btn.context)
        }
            Toast.makeText(btn.context, if(isChecked) spots.ID.toString() else "do something", Toast.LENGTH_SHORT).show()
        render(spots)
        }

It generates a couple of fragments from a list/Or card fragements which have a like button How it looks the above function should return the ID of the selected card/fragment and modify it's like counter below.

The like counter and the images and name are provided to it from a list which the function should modify using the ID parameter in the list, noted that the list is in another class

List:

var spotsList = mutableListOf (
        Spots(
            "Volcan Cerro Negro1",
            0,
            "https://www.visitcentroamerica.com/wp-content/uploads/2020/02/ver-centroamerica-nicaragua-volcán-cerro-negro-08.jpg",
            1
        ),
        Spots(
            "Volcan Cerro Negro2",
            0,
            "https://static.dw.com/image/56796908_403.jpg",
            2
        ),
        Spots(
            "Volcan Cerro Negro3",
            0,
            "https://ichef.bbci.co.uk/news/624/amz/worldservice/live/assets/images/2015/12/02/151202170824_la_ltima_vez_que_el_momotombo_entr_en_actividad_eruptiva_fue_en_1905_624x351_ap_nocredit.jpg",
            3
        ),
        Spots(
            "DC",
            0,
            "https://cursokotlin.com/wp-content/uploads/2017/07/wonder_woman.jpg",
            4
        )

Tried to check if it was reading the ID correctly , seems stuck on the ID 1 and doesn't register other like buttons like this , however hen using the toast function it seems to know to which ID the button belongs to.

Thx in Advanced :)

CodePudding user response:

I don't know what you're trying to do, but the Toast isn't checking anything to do with the button - whatever spots is (it's defined outside of this button's click listener), it's just printing the ID of that. If all your listeners refer to that variable, they're all gonna see the same thing, with the same ID

Also find { spots.ID.equals(spots.ID) } is using spots again and comparing it to itself, so that predicate always returns true and you get the first item in the list. Then you set its likes to spots.likes 1 instead of incrementing its own likes value.

You probably want to do this, assuming spots is a Spot you're trying to find in the list:

// 'it' is the default name for the variable being passed in,
// i.e. the item in the list you're currently checking - you're looking for a match
spotsProvider.spotsList?.find { it.ID.equals(spots.ID) }?.likes  = 1

This might be enough to fix it, if each button is in its own thing (like a ViewHolder) with its own value for spots

  • Related