-1

I am making a simple order app. I have a recyclerview that contains products with their id, price, and quantity. When I click on a product, it opens the product details activity and I want it to display the product name and price in two textviews. I'm not sure how to transfer the data from the recyclerview to display in the textviews of product details activity.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • This approach (also as shown in the one answer) is not exactly the suggested way... all one needs is the ID. – Martin Zeitler Apr 29 '21 at 03:38
  • Did you try searching google or this site? For example: https://stackoverflow.com/questions/40541581/recyclerview-click-to-pass-data-to-new-activity – dominicoder Apr 29 '21 at 03:48

3 Answers3

1

Simple code for Pass Data

FirstActivity

    Intent sendIntent = new Intent(FirstActivity.this , SecondActivity.class);
    sendIntent.putExtra("firstData" , "HelloWorld1");
    sendIntent.putExtra("secondData" , "HelloWorld2");
    sendIntent.putExtra("thirdData" , "HelloWorld3");
    startActivity(sendIntent);

SecondActivity

    String firstData = getIntent().getExtras().getString("firstData");
    String secondData = getIntent().getExtras().getString("secondData");
    String thirdData = getIntent().getExtras().getString("thirdData");
    textview1.setText(firstData);
    textview2.setText(secondData);
    textview3.setText(thirdData);
Ganesh MB
  • 1,109
  • 2
  • 14
  • 27
0

You can pass the data in the Intent itself by adding them as extras.

    Intent intent = new Intent(context, YourActivity.class);
    String id = "your_id";
    int quantity = 0;
    double price = 0.0d;

    intent.putExtra("Id",id);
    intent.putExtra("Quantity",quantity);
    intent.putExtra("Price",price);

And then in your next Activity, you can extract these from Intent.

 Bundle extras = getIntent().getExtras();
   if(extras != null) {
    String id = extras.getString("Id");
    int quantity = extras.getInt("Quantity");
    double price = extras.getDouble("Price");
}  
Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
0

Simple code for Pass Data Recyclerview TO NextActivity

RecycleView Adpter

public void onBindViewHolder(final MyViewHolder holder, final int position) {
   holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         Intent sendIntent = new Intent(FirstActivity.this , SecondActivity.class);
         sendIntent.putExtra("firstData" , holder.textview.getText().toString());
         sendIntent.putExtra("secondData" ,  holder.textview1.getText().toString());
         sendIntent.putExtra("thirdData" ,  holder.textview2.getText().toString());
         startActivity(sendIntent);
        }
    });
}

Second Activity

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_frame);
    String firstData = getIntent().getExtras().getString("firstData");
    String secondData = getIntent().getExtras().getString("secondData");
    String thirdData = getIntent().getExtras().getString("thirdData");
    textview1.setText(firstData);
    textview2.setText(secondData);
    textview3.setText(thirdData);}