如何从微调器中检索数据并进行处理?

时间:2018-09-13 02:27:33

标签: android spinner settext onitemselectedlistener

这是我的代码中出错的部分(我省略了所有变量声明以使其简短):我需要创建两个微调器并从中检索值,例如value1和value2。然后,我需要计算出带有value1和value2的值,并且输出应该显示在屏幕上。每当我从微调器中选择(新)项目时,都需要更新此输出。但是,我的代码根本不会将值从微调器传递到我的Java代码。谁能帮我吗?非常感谢!

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sdpsalary_comp);

    //create a spinner called Spinner1
    Spinner1= (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
            R.array.cities, android.R.layout.simple_spinner_item);

    //cities is defined as string-array in XML    
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner1.setAdapter(adapter1);
    value1 = Spinner1.getSelectedItem().toString();

    //create another spinner called Spinner2
    Spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
            R.array.cities, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner2.setAdapter(adapter2);
    value2 = Spinner2.getSelectedItem().toString();
}

// I need to update the output field whenever I changed the content from the spinners
public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
    String output;
    switch(parent.getId()){
        case R.id.spinner1:
            value1 = Spinner1.getSelectedItem().toString();
            Output = ((TextView)findViewById(R.id.out));
            output=Target(base, value1, value2, cost); 
    //variables base, cost and method Target() are defined somewhere else
            Output.setText(output);
            break;
        case R.id.spinner2:
            value2 = Spinner2.getSelectedItem().toString();
            Output = ((TextView)findViewById(R.id.out));
            output=Target(base, value1, value2, cost); //base and cost are defined somewhere else
            Output.setText(output);
            break;
    }
}

public void onNothingSelected(AdapterView<?> parent) {
}

这是我的XML的一部分:

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="252dp"
    android:layout_height="46dp"
    android:layout_marginTop="32dp"
    android:spinnerMode="dropdown"
    app:layout_constraintBottom_toTopOf="@+id/NewCity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.272"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/CurrentCity"
    app:layout_constraintVertical_bias="0.293" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="250dp"
    android:layout_height="41dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:spinnerMode="dropdown"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.196"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/NewCity"
    app:layout_constraintVertical_bias="0.191" />
<TextView
    android:id="@+id/out"
    android:layout_width="wrap_content"
    android:layout_height="17dp"
    android:layout_marginStart="44dp"
    android:layout_marginTop="36dp"
    app:layout_constraintStart_toEndOf="@+id/TargetSalary"
    app:layout_constraintTop_toBottomOf="@+id/newCity" />

这是我添加了spinner.setOnItemSelectedListener(this);之后的日志记录

09-10 00:25:37.938 7797-7797/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.gatech.seclass.sdpsalarycomp, PID: 7797
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.gatech.seclass.sdpsalarycomp/edu.gatech.seclass.sdpsalarycomp.SDPSalaryCompActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.Arrays$ArrayList.get(Arrays.java:66)
    at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:344)
    at android.widget.AdapterView.getItemAtPosition(AdapterView.java:783)
    at edu.gatech.seclass.sdpsalarycomp.SDPSalaryCompActivity.onCreate(SDPSalaryCompActivity.java:60)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3 个答案:

答案 0 :(得分:0)

请在onCreate()方法中添加以下行,

spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);

如果没有这些内容,微调框将无法监听点击事件。

答案 1 :(得分:0)

像这样在onItemSelected中更改对所选值的获取:

// Change this
value1 = Spinner1.getSelectedItem().toString();

// to this
value1 = String.valueOf(parent.getItemAtPosition(pos));

还可以初始化Output TextView,可以在onCreate上进行设置。

Output = ((TextView)findViewById(R.id.out)); // move this on your onCreate

答案 2 :(得分:-1)

这里是在Android中使用Spinner的示例

         String[] spinnerData=new String["A","B","C"];

            ArrayAdapter<String> itemsArray=new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_dropdown_item,spinnerData);
        Spinner spinner1=(Spinner)view.findViewById(R.id.spinner1);       
        spinner1.setAdapter(itemsArray);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedItem=String.valueOf(adapterView.getItemAtPosition(i));

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        Spinner spinner2=(Spinner)view.findViewById(R.id.spinner2);       
        spinner2.setAdapter(itemsArray);

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedItem=String.valueOf(adapterView.getItemAtPosition(i));

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });