-3

How to open different activities with Recycle view items using on click listeners.Help me i am the beginner. my code is

Main Activity: Main Activity

Adapter

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
dev
  • 17
  • 1
  • 6
  • 2
    Its already answer : https://stackoverflow.com/questions/37186805/start-new-activity-with-onclick-in-recyclerview?noredirect=1&lq=1 – Chetan Ansel Oct 10 '18 at 06:50
  • 2
    Possible duplicate of [Start new Activity with onClick() in RecyclerView](https://stackoverflow.com/questions/37186805/start-new-activity-with-onclick-in-recyclerview) – Mohammed Atif Oct 10 '18 at 06:55

4 Answers4

1

solution.

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> countries;

public DataAdapter(ArrayList<String> countries) {
    this.countries = countries;
}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardlay, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int i) {

    viewHolder.tv_country.setText(countries.get(i));
    viewHolder.tv_country.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            if(countries.get(i).equalsIgnoreCase("India")){
                Intent intent= new Intent(context, First.class);
                context.startActivity(intent);
            }

            else if(countries.get(i).equalsIgnoreCase("Germany")){
                Intent intent= new Intent(context, Second.class);
                context.startActivity(intent);
            }
        }
    });
}

@Override
public int getItemCount() {
    return countries.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_country;
    public ViewHolder(View view) {
        super(view);

        tv_country = (TextView)view.findViewById(R.id.tv_country);
    }
}

}

dev
  • 17
  • 1
  • 6
0
viewHolder.tv_country.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(countries.get(i).equalsIgnoreCase("India")){

               // open activiy here
            }

            else if(countries.get(i).equalsIgnoreCase("Germany")){
               // open activiy here
            }

            .
            .
            .
    });
Harish Rawal
  • 226
  • 2
  • 15
  • thanks, but it does not going to another activity. showing error : Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference my code for intent Intent intent= new Intent(context, First.class); context.startActivity(intent); – dev Oct 10 '18 at 07:22
  • "i'm happy to help you. – Harish Rawal Oct 10 '18 at 11:31
0

Send the Activity name in adapterdatalist. And in OnClickListener start the activity by calling startMyActivity method as below:

holder.itemView.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
              startMyActivity(dataSet.get(listPosition));
       }
                    });

here dataset is my list of activity name. And below is the code to call the activity by name

 private void startMyActivity(String ActivityName){
        String activityToStart = ActivityName;
        try {
            Class<?> c = Class.forName(activityToStart);
            Intent intent = new Intent(mContext, c);
            mContext.startActivity(intent);
        } catch (ClassNotFoundException ignored) {
            Log.e("no activity","Activity not found");
        }
    }
kumud kala
  • 117
  • 6
0

Implement an interface in your view(Activity/Fragment) and pass it to the RecyclerView Constructor. Then on a views onclick event just call the interface's method and handle the click in your activity or Fragment.

For eg-

public YourAdapter( ArrayList<RESULT> list, IRecyclerItemClickListener listener) {
    this.list = list;
    this.listener = listener;
}

In ViewHolder

yourView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    listener.onclik(yourview,position);
}

In your implemented Activity or Fragment

@Override
public void onViewClicked(View view,int position) {

    switch(view.getId()){
     case R.id.yourview:startActivity(new Intent(this,ActivityB.class))
 }
}
Aman Rawat
  • 375
  • 1
  • 11