-2

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?

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 1
    Does this answer your question? [How to open a different activity on recyclerView item onclick](https://stackoverflow.com/questions/28767413/how-to-open-a-different-activity-on-recyclerview-item-onclick) – Anup Lal Jan 08 '20 at 08:26

3 Answers3

0

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);
Ashim Ghimire
  • 125
  • 1
  • 10
0

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.

Anup Lal
  • 59
  • 1
  • 12
0

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



}

}

Faizan Mir
  • 310
  • 5
  • 15