从onItemSelctedListener中传递变量

时间:2012-10-18 16:00:51

标签: java android onclicklistener

我在setOnItemSelectedListeners内声明了不同的变量(temperature_var,size_var,yolk_var),我想将这些变量传递给按钮的另一个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

    }
}

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

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

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

这家伙只使用一个Spinner,但是你可以看到Spinner将其ItemSelectedListener设置为activity(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;
    }
}