0

I am working on a recipe or biography kind of app where I need to populate my activity only with different content but in the same activity layout.

I couldn't find a way I don’t need to create tons of different activities for each item selected in my RecyclerView.

Thank you in advance.

1 Answers1

0

Let's try to say you cant to create a new activity based on some text in the custom list item and this is your custom view holder class

public static class CustomViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

public TextView titleView;

public ViewHolder(View itemLayoutView) {
    super(itemLayoutView);
    titleView = (TextView) itemLayoutView.findViewById(R.id.item_title);
}

@Override
public void onClick(View v) {

    }
}

In your onClick() method,Start an activity and pass in some extras like

@Override
public void onClick(View v) {
     Intent myIntent = new Intent(view.getContext(),ItemActivity.class);
         myIntent.putExtra("title",titleView.getText());
         startActivity(myIntent);

    }

Then create an activity class file that will represent your ItemActivity in my case ItemActivity.java

and in the onCreate() method, we can get our extras and use it the way we like.

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_item);

    String title =this.getIntent().getStringExtra("title");

 }

You can now may be set it up as the title of your new activity actionbar likeactionBar.setTitle(title);

Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
  • Thank you so much. But how can I populate activity with different contents based on Recycler View Selection. What ı mean that is I am trying to make a biography app. There will be a lot of RecyclerView Items including great people and their pictures and names. I need to find a way to populate their life story in an activity without creating tons of activities. – AHMET ZIYA YAVUZ Nov 14 '15 at 11:05
  • Just use the same concept like instead of myIntent.putExtra("title",titleView.getText()); you may also use myIntent.putExtra("intVariableName", intValue); for integers like for drawable_resource_id.You can also pass lists and so much more – Collins Abitekaniza Nov 14 '15 at 11:27