2012-05-10 56 views
0

我正在製作一個應用程序,您可以使用單選按鈕和複選框從不同的事物中進行選擇。所以我想知道如何讓選定的數據出現在另一項活動中?數據使用單選按鈕/複選框從一個活動顯示在另一個?

這是你選擇你想要什麼咖啡的班級。

package com.coffee2go.coffee2go; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

public class coffee2 extends Activity { 


    private RadioGroup choosecoffee; 
    private Button order; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.coffee2); 

     choosecoffee = (RadioGroup) findViewById(R.id.choosetypeofcoffee); 
     order = (Button) findViewById(R.id.order_coffee); 

     addListenerOnButton(); 

    } 

    private void addListenerOnButton() { 
     // TODO Auto-generated method stub 

     order.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       startActivity(new Intent(coffee2.this, order.class)); 

      } 
     }); 

     choosecoffee.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       startActivity(new Intent(coffee2.this, order.class)); 
      } 
     }); 

    } 
}' 

這是我希望信息出現的類。該信息還應該被保存下來到MySQL數據庫在Web服務器上000webhost.com

'package com.coffee2go.coffee2go; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.CheckBox; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

public class order extends Activity { 
    private RadioGroup choosecoffee; 
    private RadioButton bryggkaffe, latte, espresso; 

    private RadioGroup sizeofcoffee; 
    private RadioButton small, big; 

    private CheckBox sugar, milk; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.order); 

     TextView orderlist = (TextView) findViewById(R.id.orderlist); 
     TextView pricelist = (TextView) findViewById(R.id.pricelist); 
     choosecoffee = (RadioGroup) findViewById(R.id.choosetypeofcoffee); 
     bryggkaffe = (RadioButton) findViewById(R.id.bryggkaffe); 
     latte = (RadioButton) findViewById(R.id.latte); 
     espresso = (RadioButton) findViewById(R.id.espresso); 

     sizeofcoffee = (RadioGroup) findViewById(R.id.choosecoffeesize); 
     small = (RadioButton) findViewById(R.id.small); 
     big = (RadioButton) findViewById(R.id.big); 

     sugar = (CheckBox) findViewById(R.id.sugar); 
     milk = (CheckBox) findViewById(R.id.milk); 

     if (bryggkaffe.isChecked() == true) { 
      orderlist.append("Bryggkaffe\n"); 

     } 
     if (latte.isChecked() == true) { 
      orderlist.append("Latte\n"); 
     } 

     if (espresso.isChecked() == true) { 
      orderlist.append("Espresso\n"); 
     } 
     if (small.isChecked() == true) { 
      orderlist.append("Liten\n"); 
     } 

     if (big.isChecked() == true) { 
      orderlist.append("Stor\n"); 
     } 

     if (sugar.isChecked() == true) { 
      orderlist.append("Socker\n"); 
     } 

     if (milk.isChecked() == true) { 
      orderlist.append("Mjölk\n"); 

     } 

    }' 

有什麼辦法,使這個?我是否需要建立連接類和php?

+0

用很少的評論發佈你的整個代碼可能不會有幫助 - 發佈你遇到問題的地方。你想要做什麼來獲得你想要的東西? –

回答

0

您可以傳遞Intent中的數據來創建第二個活動。

這裏是你想從你的第一個活動叫什麼:

Intent orderIntent = new Intent(coffee2.this, order.class)); 
orderIntent.putExtra("CoffeeType", someVariableThatHoldsCoffeeType); 
startActivity(orderIntent); 

在你的第二個活動(訂單),你可以在OnCreate(...)方法檢索來自意圖咖啡類型:

Intent callingActivityIntent =getIntent(); 
String coffeeType=sender.getExtras().getString("CoffeeType"); 
//Do something with the coffeeType variable 
... 

下面是如何將數據傳遞從開始到結束的教程: http://mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/

0

Gophermofer看法是正確的,但似乎喲你的應用程序像這樣運行coffee2 - >order - >coffee2。因此,不要使用startActivity()啓動order類,您應該將相同的Intent傳遞給startActivityForResult()。

startActivityForResult(new Intent(coffee2.this, order.class), 1); 

一旦用戶在order完成它們的順序,使用的setResult()這樣的:

Intent intent = new Intent(); 
intent.putExtra("Order", orderlist.getText().toString()); 
... // continue this for everything you want to pass back 
setResult(10, intent); 
finish(); 

現在回到coffee2設置的onActivityResult()函數:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == 1 && resultCode == 10) { 
     String orderlist = data.getString("Order"); 
     // Process the order 
    } 
} 

瞧!通過信息to and from activities

相關問題