Android create a spinner with items that have a hidden value and display some text?

后端 未结 5 1530
既然无缘
既然无缘 2020-12-10 11:06

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

相关标签:
5条回答
  • 2020-12-10 11:27

    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>
    
    0 讨论(0)
  • 2020-12-10 11:34

    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

    0 讨论(0)
  • 2020-12-10 11:41

    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();
      
    0 讨论(0)
  • 2020-12-10 11:47

    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());
        ...
    }
    
    0 讨论(0)
  • 2020-12-10 11:50

    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
    
                }
            });
    
    0 讨论(0)
提交回复
热议问题