2017-08-26 60 views
-2

这些图片显示了我在想什么。ANDROID STUDIO:将数据从第一种形式传输到第二种形式的列表视图

enter image description here

btn_view_cart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       activity2 object = new activity2(); 
       Intent MainActivity = new Intent(MainActivity.this,activity2.class); 
       Bundle b = new Bundle(); 
       b.putString(object.temp, ""); 
       MainActivity.putExtras(b); 
       startActivity(MainActivity); 
       finish(); 

      } 
     }); 

这里是我的看法车代码

Here is my second activity. And I need to display the name, description, price, quantity and the total of all the orders whenever the user clicks the add to cart button.

我需要使用意图的名称,描述,价格和数量转移到第二个活动,并把数据在ListView中。我怎样才能做到这一点?

+0

我投票关闭这一问题作为题外话,因为这是没有任何努力由唯一链接的问题提问者提出一个适当的问题。 –

+0

我是一个新用户。我很抱歉。 –

+0

没问题,我已经编辑了你的问题,至少确定了一些可以看到的东西,但我们也需要你的问题的具体文字描述,所以请点击android标签下方的编辑链接编辑你的问题。 –

回答

0

使用Intent来传输数据。

但是对于传输数据使用列表Bundle和同步您的POJO类。

Intent intent = new Intent(MainActivity.this, CartActivity.class) 
intent.putExtra("quantity", quantity); 
intent.putExtra("price", price); 
intent.putExtra("description", description); 
startActivity(intent); 

在CartActivity的onCreate方法:当你把

String name = getIntent().getStringExtra("name"); 
String description = getIntent().getStringExtra("description"); 
double price = getIntent().getDoubleExtra("price", 0); 
int quantity = getIntent().getIntExtra("quantity", 0); 

必须使用相同的密钥和检索每个值

1
在MainActivity

)和getStringArrayListExtra()将所有的订单放入列表中。 在你调用方法:

ArrayList<String> orders = new ArrayList<>(); 
     orders.add(whatever1); 
     orders.add(whatever2); 
     //....// 
     orders.add(whateverN); 

     Intent intent = new Intent(MainActivity.this,Next.class); 
     intent.putStringArrayListExtra("ORDERS",orders); 

下一个类添加的OnCreate中:

ArrayList<String> orders = Intent.getIntent().getStringArrayListExtra("ORDERS"); 
0

您还可以使用putStringArrayListExtra(

+0

什么应该在(whatever1)? –

+0

在你的情况下你想添加到你的订单列表中的字符串。 –

+0

btn_meal3.setOnClickListener(新View.OnClickListener(){ @Override 公共无效的onClick(查看视图){ imgview_1.setImageResource(R.drawable.mcchik); display_product_name.setText( “MC鸡肉粉”); display_product_desc .setText( “世界最好的鸡”); 双meal3_price = 49.99; 字符串meal3_string_price = Double.toString(meal3_price); display_product_price.setText(meal3_string_price); } }); 我的代码按钮3 –

相关问题