0

我在其中声明了不同的变量(temperature_var、size_var、yolk_var),setOnItemSelectedListeners并且我想将这些变量传递给另一个setOnClickListener按钮。

问题是我无法访问其中的变量(示例temperature_var),setOnClickListener因此 Eclipse 告诉我在本地声明变量。我不确定如何在onclick/中传递这些变量OnItemSelectedListeners。完整的代码如下所示:

package com.example.eggtimer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Call Spinners & Buttons
        Spinner temperature = (Spinner) findViewById(R.id.spinnerTemperature);
        Spinner size = (Spinner) findViewById(R.id.spinnerSize);
        Spinner yolk = (Spinner) findViewById(R.id.spinnerYolk);

        // Register Spinners & Buttons
       temperature.setOnItemSelectedListener(this);
       size.setOnItemSelectedListener(this);
       yolk.setOnItemSelectedListener(this);


        // Call Adapters
         ArrayAdapter<String> adapter_temperature = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.temperature));
            // Sets the list template
            adapter_temperature.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         ArrayAdapter<String> adapter_size = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.size));
            // Sets the list template
            adapter_size.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         ArrayAdapter<String> adapter_yolk = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.yolk));
            // Sets the list template
            adapter_yolk.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        

        // Combine Adapters with Spinners   
        temperature.setAdapter(adapter_temperature);
        size.setAdapter(adapter_size);
        yolk.setAdapter(adapter_yolk);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
            long arg3) {
        Toast.makeText(getApplicationContext(), "It works", Toast.LENGTH_LONG).show();

        // Call Adapters again ==> I do not know why this is necessary but otherwise the array adapters can not be found within onItemSelected
         ArrayAdapter<String> adapter_temperature = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.temperature));
            // Sets the list template
            adapter_temperature.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         ArrayAdapter<String> adapter_size = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.size));
            // Sets the list template
            adapter_size.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         ArrayAdapter<String> adapter_yolk = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.yolk));
            // Sets the list template
            adapter_yolk.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   

        // Call next button
         Button next = (Button) findViewById(R.id.buttonNext);

        // Debug Variables
        TextView debug1 = (TextView) findViewById(R.id.textViewDebug1);
        TextView debug2 = (TextView) findViewById(R.id.textViewDebug2);
        TextView debug3 = (TextView) findViewById(R.id.textViewDebug3);

        switch(v.getId()) {
        case R.id.spinnerSize:

            Toast.makeText(getApplicationContext(), "Spinner selected", Toast.LENGTH_LONG).show();


            break;
        default:
            break;
        }


    }

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

    }
}
4

2 回答 2

0

如果要在侦听器之外使用它们,请在 onclick 侦听器之外声明变量。

只需将它们放入活动中,所有不同的听众都可以访问它们。

另外,如果他们所做的只是在视图上设置文本,您可以让活动实现 OnItemSelectedListener 并让它们都进入相同的 onItemSelected 方法。然后您可以调用list.getItemAtPosition()并将其转换为字符串。它稍微简化了你的代码。

于 2012-10-18T16:14:32.627 回答
0

基本上,声明为 final 的字段在第一次设置后无法更改。

因此,为了能够从 OnItemSelectedListeners(和其他侦听器)中获取字段,我建议您选择让 Activity 实现 OnItemSelectedListener,而不是像在代码中那样使用匿名方法。

看看这个例子:http ://start-jandroid.blogspot.dk/2011/01/android-spinner-example.html

这家伙只使用了一个 Spinner,但正如您所见,Spinner 将其 ItemSelectedListener 设置为活动(this)。

现在在 Activity 的 onItemSelected 监听器中,您应该像这样创建一个开关:

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
    long id) {
    switch(v.getId) {
    case R.id.mySpinner1:
        // Do stuff when the mySpinner1 has been selected like changing some text on a button like so:
        someString = "myNewString";
        myButton1.setText(someString);
        break;
    case R.id.mySpinner2:
        // Do stuff when the mySpinner2 has been selected like changing some text on another button like so:
        someOtherString = "mybrandNewString";
        myButton2.setText(someOtherString);
        break;
    default:
        break;
    }
}
于 2012-10-18T16:21:36.853 回答