使用单个按钮/复选框从一个活动显示在另一个活动中的数据?

时间:2012-05-10 14:57:04

标签: android mysql

我正在制作一个应用程序,你可以使用单选按钮和复选框从不同的东西中选择。所以我想知道如何让所选数据出现在另一个活动中?

这是您选择所需咖啡的课程。

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

    }
}'

这是我想要显示信息的类。该信息还应保存到webserver 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?

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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