admin管理员组文章数量:1405905
I m building an Android application with Kotlin. I m using a custom RecicleView to display a list of item in my Activity.
So this is my custom recicleview class:
class DeviceDiscoveredViewAdapter(context: Context, deviceDiscoveredList: List<BluetoothDevice>,
pairedDevice: List<Device>) : ArrayAdapter<BluetoothDevice>(context, R.layout.device_dicovered_list_item, deviceDiscoveredList) {
@SuppressLint("MissingPermission")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var view = convertView
val data = getItem(position)
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.device_dicovered_list_item, parent, false)
}
val pathImage = view!!.findViewById<ImageView>(R.id.device_discovered_image)
val name = view.findViewById<TextView>(R.id.device_discovered_name)
val macAddress = view.findViewById<TextView>(R.id.device_discovered_mac_address)
val paired = view.findViewById<TextView>(R.id.device_discovered_paired)
if (data != null) {
name.text = "${data.name ?: "N/A"}"
macAddress.text = "${data.address ?: "N/A"}"
if(data.getBondState() == BluetoothDevice.BOND_BONDED){
paired.text = "Associato"
}else{
//devo verificare che non sia già stato associato.
}
//image handling
if (data.name!=null && data.name.contains(context.getString(R.string.device_zephyr_abbreviation), ignoreCase = true)){
pathImage.setImageResource(R.drawable.zephyr_pairing)
}else if (data.name!=null && data.name.contains(context.getString(R.string.device_cnr_abbreviation), ignoreCase = true)){
pathImage.setImageResource(R.drawable.sensore_cnr)
}
}
return view
}
I need to use pairedDevice field into fun getView. What can I change to use it?
I m building an Android application with Kotlin. I m using a custom RecicleView to display a list of item in my Activity.
So this is my custom recicleview class:
class DeviceDiscoveredViewAdapter(context: Context, deviceDiscoveredList: List<BluetoothDevice>,
pairedDevice: List<Device>) : ArrayAdapter<BluetoothDevice>(context, R.layout.device_dicovered_list_item, deviceDiscoveredList) {
@SuppressLint("MissingPermission")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var view = convertView
val data = getItem(position)
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.device_dicovered_list_item, parent, false)
}
val pathImage = view!!.findViewById<ImageView>(R.id.device_discovered_image)
val name = view.findViewById<TextView>(R.id.device_discovered_name)
val macAddress = view.findViewById<TextView>(R.id.device_discovered_mac_address)
val paired = view.findViewById<TextView>(R.id.device_discovered_paired)
if (data != null) {
name.text = "${data.name ?: "N/A"}"
macAddress.text = "${data.address ?: "N/A"}"
if(data.getBondState() == BluetoothDevice.BOND_BONDED){
paired.text = "Associato"
}else{
//devo verificare che non sia già stato associato.
}
//image handling
if (data.name!=null && data.name.contains(context.getString(R.string.device_zephyr_abbreviation), ignoreCase = true)){
pathImage.setImageResource(R.drawable.zephyr_pairing)
}else if (data.name!=null && data.name.contains(context.getString(R.string.device_cnr_abbreviation), ignoreCase = true)){
pathImage.setImageResource(R.drawable.sensore_cnr)
}
}
return view
}
I need to use pairedDevice field into fun getView. What can I change to use it?
Share Improve this question asked Mar 7 at 8:34 bircastribircastri 2,16714 gold badges55 silver badges132 bronze badges3 Answers
Reset to default 0Just add private val
before pairedDevice: List<BluetoothClass.Device>
in constructor and boom you can now use pairedDevice
in getView
So It will look like
add modifier val to pairedDevice:
class DeviceDiscoveredViewAdapter(context: Context, deviceDiscoveredList: List<BluetoothDevice>,
val pairedDevice: List<Device>) : ArrayAdapter<BluetoothDevice>(context, R.layout.device_dicovered_list_item, deviceDiscoveredList)
kotlin:
class ExampleClass(val a:Int,b:Int){
val count = a + b
}
same java:
public class ExampleClass{
public final int a;
public final int count;
public ExampleClass(int a,int b){
this.a = a;
this.count = a+b;
}
}
Just use private val pairedDevice:List<Device>
instead of pairedDevice:List<Device>
inside the constructor of the class as it will become accessible in whole class.
本文标签: androidRecicleViewAdapter customStack Overflow
版权声明:本文标题:android - RecicleViewAdapter custom - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744941666a2633524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论