我正在开发一个应用程序,它将根据用户的卡路里需求随机生成一个菜单。在我的第一个活动中,我询问用户的输入,并收集计算每日卡路里需求所需的信息。所需的卡路里是我需要转移到我的第二个活动的双倍值,以便准确地生成一个菜单。我一直在寻找我能找到的任何来源,但我没有找到任何可行的解决办法。正如您将看到的,我尝试使用getIntent(),并且尝试在第二个活动中调用一个方法,但是在运行后该值没有更新,即使正确计算了卡路里,该方法也总是返回0.0。我觉得这可能是一个简单的解决办法,但我不明白我做错了什么。
下面的代码:
package com.jfreitas2.freitas_project1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Intent good = new Intent(MainActivity.this, Main2Activity.class);
public double weight1, calNeeds;
RadioButton mrad, frad;
EditText weight, age;
CheckBox kg, lb, sed, mod, act;
String gender;
int age1;
TextView test;
double cals = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find all Id's related to the check boxes and radio buttons
mrad = (RadioButton) findViewById(R.id.malerad);
frad = (RadioButton) findViewById(R.id.femalerad);
lb = (CheckBox) findViewById(R.id.lbCheck);
kg = (CheckBox) findViewById(R.id.kgCheck);
sed = (CheckBox) findViewById(R.id.checkSed);
mod = (CheckBox) findViewById(R.id.checkMod);
act = (CheckBox) findViewById(R.id.checkAct);
addListenerToCheckBoxes(); //adds listeners to all check boxes and radio buttons
checkBoxOnlyOne(); //ensures only one checkbox/radio button can be checked within each pair
addListenerOnButton(); //runs program to determine ideal body weight
}
public void addListenerOnButton()//runs program to determine ideal body weight
{
//finds calculate button's ID
Button btn = (Button) findViewById(R.id.etbutton);
//run everything below this when button is clicked (calculate ideal body weight)
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calNeeds = getUserCalNeeds();
good.putExtra("cals", calNeeds);
startActivity(good);
}
});
}
public void addListenerToCheckBoxes()//adds listeners to all check boxes and radio buttons to set them checked when clicked
{
lb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lb.setChecked(true);//set to checked
}
});
kg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
kg.setChecked(true);//set to checked
}
});
mrad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mrad.setChecked(true);//set to checked
}
});
frad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frad.setChecked(true);//set to checked
}
});
sed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sed.setChecked(true);//set to checked
}
});
mod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mod.setChecked(true);
}
});
act.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
act.setChecked(true);
}
});
}
public void checkBoxOnlyOne()//ensures only one checkbox/radio button can be checked within each pair
{
lb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
kg.setChecked(false);//change accompanying button to false
}
});
kg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
lb.setChecked(false);//change accompanying button to false
}
});
mrad.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
frad.setChecked(false);//change accompanying button to false
}
});
frad.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mrad.setChecked(false);//change accompanying button to false
}
});
sed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mod.setChecked(false);
act.setChecked(false);
}
});
mod.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sed.setChecked(false);
act.setChecked(false);
}
});
act.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sed.setChecked(false);
mod.setChecked(false);
}
});
}
public double getUserCalNeeds()
{
//retrieve ID's
weight =(EditText) findViewById(R.id.editWeight);
age = (EditText) findViewById(R.id.editAge);
//turn Strings into doubles in order to run equations
weight1 = Double.parseDouble(weight.getText().toString());
age1 = Integer.parseInt(age.getText().toString());
//check what gender was chosen for choosing an equation
if(mrad.isChecked())
gender = "male";
else if(frad.isChecked())
gender = "female";
//check if weight is kg or lb.. if it is lbs then we convert to kg
if(lb.isChecked())
weight1 = weight1*0.45359237;
if(gender == "female") {
if (age1 >= 3 && age1 <= 9) calNeeds = (22.5 * weight1) + 499;
else if (age1 >= 10 && age1 <= 17) calNeeds = (12.2 * weight1) + 746;
else if (age1 >= 18 && age1 <= 29) calNeeds = (10.7 * weight1) + 496;
else if (age1 >= 30 && age1 <= 60) calNeeds = (8.7 * weight1) + 829;
else if (age1 >= 61) calNeeds = (10.5 * weight1) + 596;
}
else if(gender == "male") {
if (age1 >= 3 && age1 <= 9) calNeeds = (22.7 * weight1) + 495;
else if (age1 >= 10 && age1 <= 17) calNeeds = (17.5 * weight1) + 651;
else if (age1 >= 18 && age1 <= 29) calNeeds = (15.3 * weight1) + 679;
else if (age1 >= 30 && age1 <= 60) calNeeds = (11.6 * weight1) + 879;
else if (age1 >= 61) calNeeds = (13.5 * weight1) + 487;
}
if(sed.isChecked())
calNeeds *= 1.2;
else if(mod.isChecked())
calNeeds *= 1.3;
else if(act.isChecked())
calNeeds *= 1.4;
return calNeeds;
}
public double getCals()
{
return calNeeds;
}
}
活动2
package com.jfreitas2.freitas_project1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class Main2Activity extends AppCompatActivity {
//MainActivity main = new MainActivity();
double calNeeds = getIntent().getDoubleExtra("cals", 0.0);
public HashMap<String, Integer> breakie = new HashMap<String, Integer>(), lunchie = new HashMap<String, Integer>(), dindin = new HashMap<String, Integer>(); //create hashmaps for food and calories
public ArrayList<String> breakArr = new ArrayList<String>(), lunchArr = new ArrayList<String>(), dinArr = new ArrayList<String>(); //arrays to fill with food to compare to hashmaps.
public ArrayList<String> bMenu = new ArrayList<String>(), lMenu = new ArrayList<String>(), dMenu = new ArrayList<String>();
public ListView bList, lList, dList;
TextView totalCal;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bList = (ListView) findViewById(R.id.breakList);
lList = (ListView) findViewById(R.id.lunchList);
dList = (ListView) findViewById(R.id.dinnerList);
totalCal = (TextView) findViewById(R.id.totalCal);
//fillMaps();
//fillArrayLists();
//fillMenus();
//totalCal.setText(""+calNeeds);
}
public void fillMaps()
{
//Fill Breakfast Map
breakie.put("2 Eggs", 156);
breakie.put("3 Strips Bacon", 129);
breakie.put("2 Sausage Links", 340);
breakie.put("1 Cup Cereal", 307);
breakie.put("1 Large Banana", 121);
breakie.put("2 Slices Wheat Bread with Peanut Butter", 326);
breakie.put("1 Cup Strawberries", 50);
breakie.put("2 Pieces French Toast", 300);
breakie.put("2 Medium Pancakes with Syrup", 232);
breakie.put("1 Chewie Granola Bar", 100);
//
//Fill Lunch Map
lunchie.put("1 Chicken Breast", 231);
lunchie.put("4 Slices Turkey Breast", 88);
lunchie.put("2 Slices Wheat Bread", 138);
lunchie.put("5 Piece Chicken Tenders", 230);
lunchie.put("1 Small Caesar Salad", 94);
lunchie.put("1 Chicken Salad Sandwich", 200);
lunchie.put("1 Small Bag of Chips", 153);
lunchie.put("1 Quesadilla", 528);
lunchie.put("1 Cup Chicken Alfredo", 415);
lunchie.put("1 Ham Sandwich", 292);
//
//Fill Dinner Map
dindin.put("1 5oz Steak", 429);
dindin.put("6oz Spinach", 40);
dindin.put("6oz Cooked Pasta", 225);
dindin.put("4oz French Fries", 365);
dindin.put("1 Hamburger", 254);
dindin.put("1 Slice Pizza", 185);
dindin.put("1 Garden Salad", 100);
dindin.put("1 Chicken Parm Dish", 400);
dindin.put("3 Chicken Wings", 300);
dindin.put("1 Potato Roasted", 161);
//
}
public void fillArrayLists()
{
//filling arraylist with food for breakfast
breakArr.add("2 Eggs"); breakArr.add("3 Strips Bacon"); breakArr.add("2 Sausage Links");
breakArr.add("2 Cups Cereal"); breakArr.add("1 Large Banana"); breakArr.add("2 Slices Wheat Toast with Peanut Butter");
breakArr.add("1 Cup Strawberries"); breakArr.add("2 Pieces French Toast"); breakArr.add("2 Medium Pancakes with Syrup");
breakArr.add("1 Chewie Granola Bar");
//
//filling arraylist with food for lunch
lunchArr.add("1 Chicken Breast"); lunchArr.add("4 Slices Turkey Breast"); lunchArr.add("2 Slices Wheat Bread");
lunchArr.add("5 Piece Chicken Tenders"); lunchArr.add("1 Small Caesar Salad"); lunchArr.add("1 Chicken Salad Sandwich");
lunchArr.add("1 Small Bag of Chips"); lunchArr.add("1 Quesadilla"); lunchArr.add("1 Cup Chicken Alfredo");
lunchArr.add("1 Ham Sandwich");
//
//filling arraylist with food for dinner
dinArr.add("1 5oz Steak"); dinArr.add("6oz Spinach"); dinArr.add("6oz Cooked Pasta");
dinArr.add("4oz French Fries"); dinArr.add("1 Hamburger"); dinArr.add("1 Slice Pizza");
dinArr.add("1 Garden Salad"); dinArr.add("1 Chicken Parm Dish"); dinArr.add("3 Chicken Wings");
dinArr.add("1 Potato Roasted");
}
public void fillMenus()
{
Random rand = new Random(10);
int x;
double currentCal = 0;
while(currentCal < calNeeds)
{
//add to breakfast menu
x = rand.nextInt();
currentCal += breakie.get(breakArr.get(x));
bMenu.add(breakArr.get(x));
//add to lunch menu
x = rand.nextInt();
currentCal += lunchie.get(lunchArr.get(x));
lMenu.add(lunchArr.get(x));
//add to dinner menu
x = rand.nextInt();
currentCal += dindin.get(dinArr.get(x));
dMenu.add(dinArr.get(x));
count++;
}
}
/*public void displayMenus()
{
bList = (ListView) findViewById(R.id.breakList);
lList = (ListView) findViewById(R.id.lunchList);
dList = (ListView) findViewById(R.id.dinnerList);
test = (TextView) findViewById(R.id.textView);
test.setText(bMenu.get(0));
ArrayAdapter<String> breakAdapter = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_list_item_1, bMenu);
bList.setAdapter(breakAdapter);
}*/
}
您正在尝试使用==
运算符比较性别字符串,该运算符测试引用的相等性而不是值。因此,没有输入if else
块,因此calneeds
变量没有得到正确的值,而是保持0.0
。您应该改用equals
方法:
if (gender.equals("female")) {
(...)
} else if (gender.equals("male")) {
(...)
}
我正在尝试在活动之间传递数据 我使用intents在常规活动之间传递数据 请考虑以下代码:: 假设我将来自名为的其他活动的数据作为意图发送到 现在,如何接收中的数据,这些数据是从获得的,然后再将其传递到中 象形表示为:: {EDIT}--如果根据答案可以这样做--不明确,因为AndroidTabRestaurantDescearchListView是一个选项卡活动
我试图将一个活动(LensActivity)中的ArrayList中的值传递给我的主活动上的TextView。在这个网站上,我找到了Intent方法并进行了实验,但似乎无法传递它,信息被提取到字符串lensString中,并传递到Intent,但在主活动中似乎没有传递或获取TextView,在一些实验中,由于getIntent在主活动中,我得到了一个空指针。 这是LensActivity的代码,它
正如您所知,在支持库23.2之后,当您将RecycerView作为wrap_content放在scrollview中时,滚动事件可以通过使用正常工作。但是,如果一个scrollview中有两个recycylerview,那么第二个recycylerview不会按其内容大小进行包装。有办法实现吗?
我正在开发一个Android应用程序,它读取NFC标签,检索NDEF消息,然后将其传递给WebView activity以显示那个URL网站。我正在检查Logcat,并且正在读取URL,但我认为URL没有被传递,因为我得到以下错误: 这是主要活动: 这是Webview activity,它应该获取URL并加载它: 我想获取从标记接收到的URL到 稍后将加载网站。谢谢你的帮助。
我正在开发一个由多个活动组成的Android应用程序,我必须在它们之间传递ab对象,但我不能通过使用意图传递它,因为对象的类没有实现可序列化,我怎么做?我无法修改我的类的源代码。谢谢:)
背景: 有两项活动 1x(主活动),1x(子活动-具有列表视图的活动) 主活动扩展了碎片活动,信息将在碎片中使用 单击主活动的菜单项时,子活动将打开 为ListView使用HashMap 我的目标: 单击项的HashMap值应发送回主活动,子活动(带有ListView)应关闭 问题: 我认为我犯了一个巨大的错误,但我无法解决它。 从onOptionsItemSelected和onActivityR