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

  • SEARCH
  • Home
  • 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 8031229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:01:24+00:00 2026-06-05T01:01:24+00:00

String.xml <string-array name=fruits> <item>Apple</item> <item>Banana</item> <item>Orange</item> <item>Pear</item> <item>Watermelon</item> <item>Mango</item> <item>Pineapple</item> <item>Strawberry</item> </string-array> <string-array name=total>

  • 0

String.xml

  <string-array name="fruits">
       <item>Apple</item>
       <item>Banana</item>
       <item>Orange</item>
       <item>Pear</item>
       <item>Watermelon</item>
       <item>Mango</item>
       <item>Pineapple</item>
       <item>Strawberry</item>
</string-array>

<string-array name="total">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
</string-array>

<string-array name="calorie">
    <item>80</item>
    <item>101</item>
    <item>71</item>
    <item>100</item>
    <item>45</item>
    <item>135</item>
    <item>80</item>
    <item>53</item>
</string-array>    

Java file:

    public class Fruit extends Activity implements OnClickListener, OnItemSelectedListener {
private TextView tvFruit, tvNo;
Context context=this;   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fruit);

    tvFruit = (TextView) findViewById(R.id.tvfruit);
    tvNo = (TextView) findViewById(R.id.tvno);


    final Spinner fruits = (Spinner)findViewById(R.id.spin_fruit);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,R.array.fruits,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    fruits.setAdapter(adapter);
    fruits.setOnItemSelectedListener(this);

    fruits.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
            // TODO Auto-generated method stub
            String selectedItem = fruits.getSelectedItem().toString();
            tvFruit.setText(selectedItem);
        }

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

    }

    });

    final Spinner num = (Spinner)findViewById(R.id.spin_no);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this,R.array.total,android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    num.setAdapter(adapter1);
    num.setOnItemSelectedListener(this);

    num.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
                // TODO Auto-generated method stub
                String selectedItem = num.getSelectedItem().toString();
                tvNo.setText(selectedItem);
            }

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

        }

        });

    Button save = (Button) findViewById(R.id.bsave);
    save.setTextColor(Color.BLUE);
    save.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Get the subject details and show it in an alertdialog
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Success");
                builder.setPositiveButton("OK", null);
                builder.setNegativeButton("View Log", new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int which) { // this part is done for the negative button 
                                                                             // if we want it to link to new intent
                        launchIntent();
                    }

                });
                builder.create().show();
            }

            //making the "View Log" button in dialog box to go to new intent FruitLog.class
            private void launchIntent(){
                Intent i = new Intent(Fruit.this, FruitLog.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }


        });

    }

so when user click 3 bananas in spinner, i want to know how to do the code for multiplication of calories. once done with the calculation, it should show 303 calories in the "total calorie" textview

so when user click 3 bananas in spinner, i want to know how to do the code for multiplication of calories. once done with the calculation, it should show 303 calories in the “total calorie” textview

can someone guide me on how to do the coding for the calculation. example, help me with 2 Apple calories and the rest i will try to figure out.thank you so much. this community have being very helpful.

  • 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-05T01:01:26+00:00Added an answer on June 5, 2026 at 1:01 am

    Using your existing code, I would try something along the line of this:

    First, change your calorie array to an integer-array. Next, add:

    public int calculateCalories() {
        int[] calorie = getResources().getIntArray(R.array.calorie);
        return Integer.parseInt((String) num.getSelectedItem()) * calorie[fruits.getSelectedItemPosition()];
    }
    

    This function should return the number of the chosen fruit times its caloric value. To be explicit you set this directly into a TextView like so:

    totalTextView.setText(String.valueOf(calculateCalories()));
    

    You will have to make num and fruits visible to the entire class, by declaring them the same way tvFruit is declared.

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

Sidebar

Related Questions

If my XML is like this: <Item> <Name>Jerry</Name> <Array> <Item> <Name>Joe</Name> </Item> <Item> <Name>Sam</Name>
I am trying to create the following example. <body> <resources> <string-array name=mytest> <item number=1>
I have this array: array (size=3) 0 => array (size=2) 'name' => string 'XML'
Iam developing one application.In that i take the string from array and replace .xml
After adding <item>170,000</items> to string.xml . It becomes so slow in building workspace, I
I need to parse XML string into an array. I have XML <group xmlns=123
We can read a string array from xml file in android like mentioned here
I am getting 0 count result when i am converting XML to string array
I have something like: $client = new Zend_XmlRpc_Client('<XML-RPC URL>'); $result = $client->call('<method name>', array(
Can I count on string arrays within the strings.xml resource file to be parsed/deserialized

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.