i am creating an app that is supposed to display bluetooth devices in a recyclerview and i want a user to be able to click the items to perform an action. for now i am just attempting to make a toast appear on click but later i would like to possibly display a dialog giving choices to pair, etc. however i am apperently missing something in my usage of onclicklistener. i am attempting to have my ViewHolder class: DeviceHolder implement View.OnClickListener
and placing the call to Toast.makeText()
inside of my onClick override. however, nothing is happening. i am sure i am just missing something minor and would appreciate help finding the problem. also i am doing this in kotlin which i am new to and if there is possibly a more efficient, kotlin type of way to do this that would also be of help. i am posting my code below. thanks in advance.
class DeviceAdapter(val mContext : Context) : RecyclerView.Adapter<DeviceAdapter.DeviceHolder>(){
val mDevices = ArrayList<BluetoothDevice>()
interface OnClickListener{
fun onClick(v: View)
}
fun updateItems(list: ArrayList<BluetoothDevice>){
mDevices.clear()
mDevices.addAll(list)
Log.d(TAG, "updating items : $mDevices")
notifyDataSetChanged()
}
fun ViewGroup.inflate(@LayoutRes res: Int, attachToRoot: Boolean = false): View{
return LayoutInflater.from(mContext).inflate(res, this, attachToRoot)
}
override fun onBindViewHolder(holder: DeviceHolder, position: Int) {
Log.d(TAG, "onBindViewHolder called!")
holder.bindItems(mDevices.get(position))
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DeviceAdapter.DeviceHolder{
Log.d(TAG, "onCreateViewHolder called!")
val v = parent!!.inflate(R.layout.device_item, false)
return DeviceHolder(v)
}
override fun getItemCount(): Int {
return mDevices.size
}
inner class DeviceHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
override fun onClick(v: View?) {
Toast.makeText(mContext, "test", Toast.LENGTH_LONG).show()
}
val nameView = itemView.findViewById(R.id.nameView) as TextView
val addrView = itemView.findViewById(R.id.addressView) as TextView
fun bindItems(btDevice: BluetoothDevice) {
Log.d(TAG, "holder created!")
nameView.text = btDevice.name
addrView.text = btDevice.address
itemView.setOnClickListener { this }
}
}
companion object {
val TAG = "Device Adapter"
}
}
here are the log messages:
10-09 00:35:50.233 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: device found!
10-09 00:35:51.795 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: device found!
10-09 00:35:56.752 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: device list : [**:B8:9A:39:1D:**, **:DF:BF:2A:F3:**]
10-09 00:35:56.752 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: updating items : [**:B8:9A:39:1D:**, **:DF:BF:2A:F3:**]
10-09 00:35:56.752 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: discovery finished
10-09 00:35:56.762 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onCreateViewHolder called!
10-09 00:35:56.774 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onBindViewHolder called!
10-09 00:35:56.774 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: holder created!
10-09 00:35:56.783 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onCreateViewHolder called!
10-09 00:35:56.786 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onBindViewHolder called!
10-09 00:35:56.786 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: holder created!