Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:34:51+00:00 2026-06-18T02:34:51+00:00

I have an application which has asks a user to input the amount of

  • 0

I have an application which has asks a user to input the amount of what he/she eats. The input field I used is an EditText. What I want to happen is when there’s a change in the edittext, it should calculate (i.e., multiply the amount by its calories like 2 amount of bread is equal to 8 calories. So the answer should be 16 calories.)

But I can’t get it to work. Please have a look on what I’ve tried:

UPDATED:

public class Bread_White extends Activity implements OnClickListener, OnItemSelectedListener {

Spinner sp;
Button calories, save, back, home;
String selected, strCalories;
TextView tv;
EditText etAmount;

int total;

RadioGroup rgMeal;
//RadioButton breakfast, ms, lunch, as, dinner, es;

String[] classes = {
        "Cornbread",
        "French Bread",
        "French Toast",
        "French Toast, low fat",
        "Italian Bread",
        "Wheat Bread",
        "Wheat Bread, low calories",
        "Wheat Bread, whole wheat"
};

//put how much calorie each food item has in this array
int[] intCalories = {188, 185, 126, 149, 81, 66, 46, 89};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.food_vegetable);

    initControls();

}

private void initControls() {
    // TODO Auto-generated method stub

    // RadioGroup 
    rgMeal = (RadioGroup) findViewById (R.id.rgSelectMeal);

    sp = (Spinner) findViewById (R.id.spFoodVegetable);
    save = (Button) findViewById (R.id.btFoodVegetableSave);
    calories = (Button) findViewById (R.id.btFoodVegetableCalories);
    back = (Button) findViewById (R.id.tabs_back);
    home = (Button) findViewById (R.id.tabs_home);
    tv = (TextView) findViewById (R.id.txtMenuHeader);
    etAmount = (EditText) findViewById (R.id.etAmount);
    tv.setText(R.string.whitebread);

    ArrayAdapter<String> array =
            new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, classes);
    sp.setAdapter(array);
    sp.setOnItemSelectedListener(this);

    back.setOnClickListener(this);
    home.setOnClickListener(this);
    save.setOnClickListener(this);

}

public void onClick(View v) {
    // TODO Auto-generated method stub

    switch( v.getId() ){
    case R.id.btFoodVegetableSave:
        String mealname = selected;

        String serving = calories.getText().toString();
        int i = Integer.parseInt(serving.replaceAll("[\\D]", ""));

        //int amount = Integer.valueOf(etAmount.getText().toString());
        //int answer  = amount * i;
        String strAnswer = String.valueOf(i);
        //calories.setText(strAnswer);

        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        String strDate = sdf.format(new Date());

        if ( ( mealname.isEmpty() || strAnswer.isEmpty() ) ){

            // call for custom toast
            viewErrorToast();
        }

        else {

        boolean didItWork = true;

        try{

            int checkedRadioButton = rgMeal.getCheckedRadioButtonId();

            switch( checkedRadioButton ){
            case R.id.rbBreakfast:
                BreakFastLog bfLog = new BreakFastLog(Bread_White.this);
                bfLog.open();
                bfLog.createEntry(mealname, strAnswer, strDate);
                bfLog.close();
                break;
            case R.id.rbMorningSnack:
                MorningSnackLog msLog = new MorningSnackLog(Bread_White.this);
                msLog.open();
                msLog.createEntry(mealname, strAnswer, strDate);
                msLog.close();
                break;
            case R.id.rbLunch:
                LunchLog lunchLog = new LunchLog(Bread_White.this);
                lunchLog.open();
                lunchLog.createEntry(mealname, strAnswer, strDate);
                lunchLog.close();
                break;
            case R.id.rbAfternoonSnack:
                AfternoonSnackLog asLog = new AfternoonSnackLog(Bread_White.this);
                asLog.open();
                asLog.createEntry(mealname, strAnswer, strDate);
                asLog.close();
                break;
            case R.id.rbDinner:
                DinnerLog dinnerLog = new DinnerLog(Bread_White.this);
                dinnerLog.open();
                dinnerLog.createEntry(mealname, strAnswer, strDate);
                dinnerLog.close();
                break;
            case R.id.rbEveningSnack:
                EveningSnackLog esLog = new EveningSnackLog(Bread_White.this);
                esLog.open();
                esLog.createEntry(mealname, strAnswer, strDate);
                esLog.close();
                break;
            }


            }
            catch(Exception e){
                didItWork = false;
                viewErrorToast();
            }finally{
                if (didItWork){
                    viewBMRSavedToast();
                }

            }
        } // end of if else statement
        break;
    case R.id.tabs_back:
        finish();
        break;
    case R.id.tabs_home:
        Intent home = new Intent(this, IHealthFirstActivity.class);
        home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(home); 
        break;
    }
}

private void viewBMRSavedToast() {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "Successful", Toast.LENGTH_SHORT).show();
}

private void viewErrorToast() {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
}

public void onItemSelected(AdapterView<?> parent, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
    selected = parent.getItemAtPosition(position).toString();


    int amount = Integer.valueOf(etAmount.getText().toString());
    total = amount * intCalories[position];
    calories.setText(strCalories);

    TextWatcher watcher = new TextWatcher(){
        public void afterTextChanged(Editable s) {
            //your business logic after text is changed
             strCalories = total + " calories";
             calories.setText(strCalories);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){
            //your business logic before text is changed
        }

        public void onTextChanged(CharSequence s, int start, int before, int count){
            //your business logic while text has changed
        }
    };

    etAmount.addTextChangedListener(watcher);



}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

Please help fix this. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:34:53+00:00Added an answer on June 18, 2026 at 2:34 am

    Declare selectedPos as class level variable

    int selectedPos = 0;
    
    public void afterTextChanged(Editable s) {
            //your business logic after text is changed
    
             int cal = intCalories[selectedPos];
             total = (Integer.parseInt(s.toString)) * cal;
    
             calories.setText(total + " calories");
        }
    

    set your spinner selected position in defined variable.

    Happy coding 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which has a Prefernces Class and I want to know
I have an application which has many icons (Buttons). I want to put a
I have one Application which has more then 5 Activity I want to know
I have an application which has a 'user.as' class and I have a popup
I have an application which has a similar interface to Visual Studio, in that
I have an application which has a Projects table and a Users table. In
We have an application which has about 15000 pages. For better SEO reasons we
I have an Erlang application which has a dependency in its deps directory on
I have an MVC application which has a set of fields for contact details
I have an iphone application which has about 4 tabs. How do I force

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.