My code:
public void onClick(View v) {
Intent i = new Intent(context.getApplicationContext(),Watch.class);
startactivity(i);
}
If I click on Recycler view then how to go for a new activity?
My code:
public void onClick(View v) {
Intent i = new Intent(context.getApplicationContext(),Watch.class);
startactivity(i);
}
If I click on Recycler view then how to go for a new activity?
Do it like this, you have to make listener from your adapter to your activity,Read this document
Intent intent = new Intent(this, Watch.class);
startActivity(intent);
Please search out the solutions before posting such basic questions, it has already been answered in the past. I have marked this question as duplicate.
This can be done easily by making the inner viewholder class implement the OnClickListenerInterface and then setting up a listener for clicks in your activity/fragment.
class BluetoothAdapter(var bluetoothDevices:ArrayList<BluetoothDevice?>,var mListener:BluetoothDeviceViewHolder.OnBluetoothDeviceClicked):
RecyclerView.Adapter<BluetoothAdapter.BluetoothDeviceViewHolder>() {
class BluetoothDeviceViewHolder(itemView: View,var listener: OnBluetoothDeviceClicked,var bluetoothDevices: ArrayList<BluetoothDevice?>) : RecyclerView.ViewHolder(itemView),View.OnClickListener {
var name= itemView.findViewById<TextView>(R.id.bluetoothDeviceName)
init {
itemView.setOnClickListener(this)
}
interface OnBluetoothDeviceClicked{
fun onBluetoothDeviceClicked(bluetoothDevice: BluetoothDevice?)
}
override fun onClick(p0: View?) {
listener.onBluetoothDeviceClicked(bluetoothDevices[adapterPosition])
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BluetoothDeviceViewHolder {
return BluetoothDeviceViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item,parent,false),mListener,this.bluetoothDevices)
}
override fun getItemCount(): Int {
return bluetoothDevices.size
}
override fun onBindViewHolder(holder: BluetoothDeviceViewHolder, position: Int) {
holder.name.text = bluetoothDevices[position]?.name
}
Your activity will look something like this
class SendAndReceiveActivity : AppCompatActivity() ,BluetoothService.OnBluetoothEventCallback {
override fun onBluetoothDeviceClicked(bluetoothDevice: BluetoothDevice?) {
var intent:Intent = Intent(this,Xyz::class.java)//This line will
//start a
// new activity when something is clicked in recycler view
}
}