2017-08-26 56 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 –

相關問題