Android 创建一个带有隐藏值的项目并显示一些文本的微调器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11343570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:05:32  来源:igfitidea点击:

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

android

提问by L2wis

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.

当用户选择他们选择的口味时,我希望返回 10 的草莓值。

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?

我来自 vb.net 背景,所以发现这难以置信地难以工作,因为我需要数组适配器和其他东西来做到这一点?

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

任何人都可以为我简化事情并分享一些代码吗?

采纳答案by sunil

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

我认为这篇文章会对你有所帮助Android: How to bind spinner to custom object list?

this question author has the same requirement as you

这个问题作者和你有一样的要求

回答by Mohsin Naeem

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

            }
        });

回答by Michael

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.

您可以实例化使用您的数据对象类型键入的 ArrayAdapter。Spinner 然后将保存您的完整数据对象而不是字符串。显示的值将由数据对象的 toString() 方法确定。

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());
    ...
}

回答by AkashG

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文件:

<?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>

回答by Joshua Pinter

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).

使用 Enums 可以保持整洁,并为您提供面向对象的界面,而无需保留数据。从它的外观来看,您对每种“口味”(例如Strawberry = 10)都使用了常量值。

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

因此,首先创建一个名为“包”或目录enums。我们会把你的一切都Enums留在那里。

Then create a new file in there called Flavour:

然后在那里创建一个名为的新文件Flavour

enums/Flavour.java

枚举/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();
    
  • 使用Flavour.values()获得香精您微调适配器的阵列。

  • toString()当微调被填充所以你的显示字符串(或者你在这个方法返回)将钱包显示将自动被调用。

  • 保存值时,您可以使用:

    ( (Flavour) spinner.getSelectedItem() ).value();