codingsandy
codingsandy

Reputation: 31

How to open new activity from clicking an item from recyclerview

how do you open a new empty activity when I click my List view item?

I got the codes from another source, I can do it with a button, but confused to do it on a custom recycler view?

This is my Mainactivity code

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Model, ViewHolder>(
                    Model.class,
                    R.layout.row,
                    ViewHolder.class,
                    mDbRef
            ) {
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {

                    viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getImage());

Upvotes: 0

Views: 2707

Answers (3)

chandan
chandan

Reputation: 117

You can set onClickListener in the onBindViewHolder function in your adapter.And use intent to navigate to another activity Like below example.

@Override
public void onBindViewHolder(final LeaderBoardAdapter.MyViewHolder holder, final int 
position) {

    //holder.newsUrl.setText(newsItemList.get(position).getUrl());
    holder.newsDescription.setText(newsItemList.get(position).getDescription());

    holder.newsUrl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext,ReadMoreActivity.class);
            intent.putExtra("title",newsItemList.get(position).getTitle());

            mContext.startActivity(intent);

       }
    });


}

Upvotes: 0

Amine Harbaoui
Amine Harbaoui

Reputation: 1295

You have to follow these steps:

1 - Pass the Context to your adapter using the Constructor

2 - In the onBindViewHolder function start your activity like that :

    holder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              Intent intent =  new Intent(context, ActivityToStart.class);
              context.startActivity(intent);
        }
    });

Upvotes: 3

c-an
c-an

Reputation: 4090

For clicking event, you need to implement an onClickListener as below:

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

(If you take advantage of 'Cntl+ENTER'. This work is much easier.)

And for the changing the screen(or Activity).

You can do it like this:

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

I hope this is helpful.

Upvotes: 0

Related Questions