2012-05-10 13 views
0

私はアプリケーションを作っています。これは、ラジオボタンとチェックボックスを使ってさまざまなものから選択できます。だから私は選択したデータを別のアクティビティに表示させる方法を考えていますか?1つのアクティビティのラジオボタン/チェックボックスを使用して別のアクティビティに表示するデータですか?

これはあなたが好きなコーヒーを選ぶクラスです。

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)); 
      } 
     }); 

    } 
}' 

これは、情報を表示するクラスです。情報は、ウェブサーバ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"); 

     } 

    }' 

上のMySQLデータベースにダウン保存する必要があり、これを作るための方法はありますか?私は接続クラスとPHPも作成する必要がありますか?

+0

非常に少ないコメントでコード全体を投稿することは、おそらく役立たないでしょう - 問題のある場所に投稿してください。あなたは何をしたいのですか? –

答えて

0

インテントのデータを渡して、2番目のアクティビティを作成することができます。ここで

あなたが最初のアクティビティから呼ぶものです:

:2番目のアクティビティ(注文)で

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を起動するのではなく、startActivityForResult()に同じIntentを渡す必要があります。

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

ユーザーは、このようsetResult()を使用し、orderで自分の順番を完了すると:今

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

関連する問題