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

    }
}'

这是我希望信息出现的类。信息还应保存到网络服务器 000webhost.com 上的 mySQL 数据库中

    '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吗?

4

2 回答 2

0

您可以在 Intent 中传递数据来创建您的第二个活动。

这是您从第一个活动中调用的内容:

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

在您的第二个活动(订单)中,您可以从 onCreate(...) 方法的 Intent 中检索 Coffee 类型:

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/

于 2012-05-10T16:04:57.157 回答
0

Gophermofer 有正确的想法,但您的应用程序似乎像这样coffee2-> order->运行coffee2。因此,与其order使用 startActivity() 启动您的课程,不如将相同的 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
    }
}

瞧!在活动之间传递信息。

于 2012-05-10T16:20:28.807 回答