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)
}
它從一個串列/或卡片片段中生成幾個片段,這些片段有一個類似的按鈕。看起來 上面的函式應該回傳所選卡片/片段的 ID 并修改它,如下面的計數器。
類似計數器以及影像和名稱是從串列中提供給它的,函式應該使用串列中的 ID 引數修改該串列,注意該串列在另一個類中
串列:
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
)
試圖檢查它是否正確讀取了 ID,似乎卡在 ID 1 上并且沒有像這樣注冊其他類似的按鈕,但是使用 toast 功能它似乎知道按鈕屬于哪個 ID。
高級中的Thx :)
uj5u.com熱心網友回復:
我不知道您要做什么,但是 Toast 沒有檢查與該按鈕有關的任何內容 - 無論spots是什么(它是在此按鈕的點擊偵聽器之外定義的),它只是列印ID那個。如果你所有的聽眾都參考了那個變數,他們都會看到相同的東西,具有相同的 ID
也再次find { spots.ID.equals(spots.ID) }使用spots并將其與自身進行比較,因此謂詞始終回傳 true 并且您獲得串列中的第一項。然后將其設定likes為spots.likes 1而不是增加自己的likes值。
您可能想要這樣做,假設您要在串列中找到一個spots:Spot
// '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
這可能足以修復它,如果每個按鈕都在它自己的東西中(比如 a ViewHolder),并且有它自己的值spots
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/468928.html
上一篇:將PandasDataframe中的每一行匯出到單獨的CSV
下一篇:將一個片段重定向到另一個片段
