[英]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.我有一个 recyclerview,其中包含带有 id、价格和数量的产品。 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.我不确定如何将数据从 recyclerview 传输到产品详细信息活动的文本视图中。
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 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.然后在你的下一个活动中,你可以从 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将 Data Recyclerview 传递给 NextActivity 的简单代码
RecycleView Adpter RecycleView 适配器
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);}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.