我在其中声明了不同的变量(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
}
}