Android:我需要帮助将一个double值从一个视图传递到另一个视图

时间:2016-09-30 02:33:27

标签: java android android-intent

我试图创建一个餐厅菜单应用程序,允许您选择不同的食物和饮料量,当您点击“总计”按钮时,它将转到第二个视图显示:“您的总数是:_____”。但是我收到了很多错误。如果有人可以帮助我,我会很感激。我对此非常陌生,因此我从本网站上的其他问题的其他答案中拼凑了一些代码。谢谢!

主要活动视图:

    package name.restaurantmenu2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

double totalPrice = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CheckBox pizzaCB  = (CheckBox)findViewById(R.id.cb_pizza);

    if(pizzaCB.isChecked()){
        pizzaCB.toggle();
    }

    CheckBox burgerCB  = (CheckBox)findViewById(R.id.cb_hamburger);

    if(burgerCB.isChecked()){
        burgerCB.toggle();
    }

    CheckBox hotdogCB  = (CheckBox)findViewById(R.id.cb_hotdog);

    if(hotdogCB.isChecked()){
        hotdogCB.toggle();
    }

    CheckBox sodaCB  = (CheckBox)findViewById(R.id.cb_soda);

    if(sodaCB.isChecked()){
        sodaCB.toggle();
    }
}

public void addSelected(View view){


    String multiple;

    CheckBox pizzaCB = (CheckBox)findViewById(R.id.cb_pizza);
    CheckBox burgerCB = (CheckBox)findViewById(R.id.cb_hamburger);
    CheckBox hotdogCB = (CheckBox)findViewById(R.id.cb_hotdog);
    CheckBox sodaCB = (CheckBox)findViewById(R.id.cb_soda);
    TextView pizzaTV = (TextView)findViewById(R.id.tv_pizzaPrice);
    TextView burgerTV = (TextView)findViewById(R.id.tv_hamburgerPrice);
    TextView hotdogTV = (TextView)findViewById(R.id.tv_hotdogPrice);
    TextView sodaTV = (TextView)findViewById(R.id.tv_sodaPrice);
    Spinner pizzaQty = (Spinner)findViewById(R.id.spinner_pizzaQuantity);
    Spinner burgerQty = (Spinner)findViewById(R.id.spinner_hamburgerQuantity);
    Spinner hotdogQty = (Spinner)findViewById(R.id.spinner_hotdogQuantity);
    Spinner sodaQty = (Spinner)findViewById(R.id.spinner_drinks);


    if(pizzaCB.isChecked()) {
        String value = pizzaTV.getText().toString();
        value = value.replace('$', ' ');
        multiple = String.valueOf(Double.parseDouble(pizzaQty.getSelectedItem().toString()) * Double.parseDouble(value));
        totalPrice++;
    }
    if(burgerCB.isChecked()) {
        String value = burgerTV.getText().toString();
        value = value.replace('$', ' ');
        multiple = String.valueOf(Double.parseDouble(burgerQty.getSelectedItem().toString()) * Double.parseDouble(value));
        totalPrice++;
    }
    if(hotdogCB.isChecked()) {
        String value = hotdogTV.getText().toString();
        value = value.replace('$', ' ');
        multiple = String.valueOf(Double.parseDouble(hotdogQty.getSelectedItem().toString()) * Double.parseDouble(value));
        totalPrice++;
    }
    if(sodaCB.isChecked()) {
        String value = sodaTV.getText().toString();
        value = value.replace('$', ' ');
        multiple = String.valueOf(Double.parseDouble(sodaQty.getSelectedItem().toString()) * Double.parseDouble(value));
        totalPrice++;
    }



}
public void onTotalClick(View view){

    Intent intent = new Intent(this, ComputedActivity.class);
    Bundle b = new Bundle();
    b.putDouble(totalPrice, double);
    intent.putExtras(b);
    startActivity(intent);
}

}

查看2

package name.restaurantmenu2;

import android.annotation.TargetApi;
import android.content.Intent;
import android.icu.text.NumberFormat;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.TextView;

public class ComputedActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "Nothing to display";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_computed2);


}

@TargetApi(Build.VERSION_CODES.N)
private void displayPrice(double number){

    Bundle b = getIntent().getExtras();
    double result = b.getDouble(totalPrice);

    TextView priceTextView = (TextView)findViewById(R.id.tv_totalPrice);
    priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));

}



}

2 个答案:

答案 0 :(得分:0)

首先,您使用bundle.putDouble(String name, double value)方法时出错了。它应该是bundle.putDouble("NameOfYourDouble", actualDouble)

double myDouble = 10.5;
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("MyDouble", myDouble);
startActivity(intent);

在Activity2中:

Intent intent = getIntent();
double myDouble = intent.getDoubleExtra("MyDouble", 0);

答案 1 :(得分:0)

当使用字符串中的空格进行解析时,可以在addSelected()中解析double to string异常,同时将$替换为' &#39 ;.尝试处理异常