简体   繁体   中英

Android Studio how to pass data from recyclerview to next activity textview? JAVA

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.

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);

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");
}  

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);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM