Search code examples
javaandroidspinnerandroid-spinner

How do I add multiple values from a Spinner to a TextView in Android?


I am building a simple grocery ordering App with firebase, where I want two spinners and a button.

  • One Spinner for the item name and item price tag. (For example - 'Chips -5rs-')
  • Second spinner for the quantity. (for Example - '5 packet')
  • A button for taking the values from the spinners and putting it to the TextView below. (One item after the other.)

For Example, after adding several items from the spinners, the textview below should look like this. -

  • Chips -5rs- 5 packet
  • Biscuits -20rs- 20 packet
  • Noodles -15rs- 2 packet

and so on..

How do I actually implement it? and if anyone could help me with actual java codes, I would be very thankful. I've also attached a picture below of how I am trying to Implement it. I am new to Android Development and I have keen interest in it.. any help would be deeply appreciated..

:) Thankyou ..

This is the Screenshot of what I am trying to approach.


Solution

  • Define your string global

    String priceTag,quantityTag;
    

    then get value from spinner like this

    yourSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
    
     @Override
     public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
     
    priceTag= yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
     }
    
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
     // TODO Auto-generated method stub
     
     }
     });
    

    same as getting value from your second spinner

    yourSpinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
    
     @Override
     public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
     
    priceTag= yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
     }
    
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
     // TODO Auto-generated method stub
     
     }
     });
    

    and show data on button click like this

    mButton.setOnClickListener(new View.OnClickListener() { 
                @Override
                public void onClick(View view) 
                { 
                    yourtextViewId.setText(priceTag " - " quantityTag);
                } 
            });