22

I'm sure this is asked plenty and i've found some questions on here similar but none really got the coin dropping for me. I'm hoping someone can help me out.

What I want to do is present the user with a dropdown (spinner) with a list of flavours Vanilla, Chocolate, Strawberry.

When the user selected the flavour of their choice the I want the value of Strawberry which is 10 to be returned.

Strawberry = 10
Chocolate = 20
Vanilla = 30

I come from a vb.net background so finding this incredibly hard to work with the fact i need arrayadapters and stuff to do it?

Could anyone simplify things for me and perhaps share some code?

L2wis
  • 294
  • 1
  • 4
  • 10

5 Answers5

15

you can try this

ArrayAdapter<String> SpinerAdapter;
         String[] arrayItems = {"Strawberry","Chocolate","Vanilla"};
         final int[] actualValues={10,20,30}; 

        SpinerAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, arrayItems);
        spinner.setAdapter(SpinerAdapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                int thePrice=actualValues[ arg2];

            }

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

            }
        });
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
11

i think this post will help you Android: How to bind spinner to custom object list?

this question author has the same requirement as you

Community
  • 1
  • 1
sunil
  • 6,444
  • 1
  • 32
  • 44
3

You can instantiate an ArrayAdapter typed with you data object type. The Spinner will then hold your complete data object instead of a String. Values displayed will be determined by the toString() method of your data objects.

Example:

List<Project> allProjects = ...;

    ArrayAdapter<Project> spinnerAdapter = new ArrayAdapter<Project>(this, android.R.layout.simple_spinner_item,allProjects.toArray(new Project[allProjects.size()]));
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(this);

...

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Log.i(AppConst.TAG, "ItemSelected:" + position + " id: " + id);
    Project selectedProject = (Project)getSpinner().getSelectedItem();
    currentTask = selectedProject.toString();
    Log.i(AppConst.TAG, "Selected Project:" + selectedProject.getId());
    ...
}
Michael
  • 192
  • 1
  • 5
2

here is the code:

        TextView textView=(TextView) findViewById(R.id.textView1);
        Spinner spinner=(Spinner) findViewById(R.id.spinner1);
        spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,quantity));
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                if(arg2==0){
                textView.setText("Strawberry = 10");
            }else if(arg2==1){
                textView.setText("Chocolate = 20");
            }else if(arg2==2){
                textView.setText("Vanilla = 30");
            }

            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

String[] quantity={"Strawberry","Chocolate","Vanilla"};

and xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>
AkashG
  • 7,868
  • 3
  • 28
  • 43
-1

Use Enums to neatly wrap everything up in.

Using Enums keeps things clean and provides you with a Object Oriented interface without having to persist the data. And by the looks of it, you are using constant values for each of your "flavours" (e.g. Strawberry = 10).

So, start by creating a "Package" or directory called enums. We'll keep all your Enums in there.

Then create a new file in there called Flavour:

enums/Flavour.java

public enum Flavour {

    STRAWBERRY("Strawberry", 10),
    CHOCOLATE("Chocolate", 20),
    VANILLA("Vanilla", 30);

    private String displayString;
    private int value;

    private Flavour ( String displayString, int value) {
        this.displayString = displayString;
        this.value   = value;
    }

    @Override
    public String toString() {
        return displayString;
    }

    public String displayString() { return displayString; }

    public String value() { return value; }

    public static Flavour fromDisplayString( String displayString) {

        if ( displayString != null ) {
            for ( Flavour flavour : Flavour.values() ) {
                if ( displayString.equalsIgnoreCase( flavour.displayString ) ) {
                    return flavour;
                }
            }
        }

        throw new IllegalArgumentException("No Flavour with display string " + displayString + " found");

    }

    public static Flavour fromValue( int value) {
        if (value != null) {
            for (Flavour flavour : Flavour.values()) {
                if (value.equals(flavour.value)) {
                    return flavour;
                }
            }
        }

        throw new IllegalArgumentException("No Flavour with value " + value + " found");
    }

}

I'll leave the rest of the adapter stuff up to you to do but the key pieces are this:

  • Use Flavour.values() to get the array of Flavours for your Spinner Adapter.

  • The toString() will automatically be called when the Spinner is populating so your display string (or whatever you return in that method) will be what's displayed.

  • When saving the value, you can use this:

    ( (Flavour) spinner.getSelectedItem() ).value();
    
Simon Rolin
  • 940
  • 13
  • 34
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245