简体   繁体   English

Android Studio 如何将数据从 recyclerview 传递到下一个活动 textview? JAVA

[英]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.

相关问题 如何在Android Studio(Java)中的另一个活动中将timepicker中的时间值传递给textView? - How to pass time value from timepicker to textView in another activity in Android studio(Java)? 如何将数据从RecyclerView传递给活动 - How pass data from RecyclerView to activity 如何将数据从 RecyclerView 传递给活动 - How to pass data from RecyclerView to activity 如何将数据从Recyclerview传递到新活动 - How to pass data from recyclerview to new activity 如何将 android 活动中的数据传递给 Java class? - How to pass data from one android activity to an Java class? Android Studio - 如何从 RecyclerView 中的 EditText 获取数据并将它们放入主要活动中的 ArrayList? - Android Studio - How to get data from an EditText in a RecyclerView and put them into an ArrayList in the main activity? 如何将结果从下一个活动设置为Android Studio中的上一个活动 - How to set results from next activity to previous activity in android studio 如何将数据从一个活动传递到下一个活动 - How to pass the data from one Activity to the next activity 如何将数据从 Recyclerview onClickLister 传递到其他 Activity - How to pass data from Recyclerview onClickLister to other Activity 如何使用 JAVA 在 android 的 RecyclerView 的其他活动中显示不同的数据 - How to display different data in other activity in RecyclerView in android using JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM