我正在尝试从使用相同阵列适配器的2个不同微调器获取2个字符串类型数据。我已经使用switch和case语句设置了onItemselectedListener()。搜索一段时间后我很确定代码是正确的。使用adapterView变量,该函数能够确定从哪个微调器中选择项目。但是仍然出现问题,当选择项目时textView不会改变公共类MainActivity extends Activity实现OnItemSelectedListener,OnClickListener {
public String[] all = { "dilshad garden", "jhilmil",
"mansarovar park", "shahadra", "welcome", "seelampur",
"shastri park", "kashmere gate", "tis hazari", "pul bangash",
"pratab nagar", "shastri nagar", "inderlok", "kanhiya nagar",
"netaji subhash place", "kohat enclave", "pitampura",
"rohini east", "rohini west", "rithala", "jahangirpuri",
"adarsh nagar", "azadpur", "model town", "gtb nagar",
"delhi university", "vidhan sabha", "civil lines", "kashmere gate",
"chandni chowk", "chawri bazar", "new delhi", "rajiv chowk",
"patel chowk", "centrial secretariat" };
Button route;
Spinner stationFrom, stationTo;
String finalTo, finalFrom, result;
TextView totv,fromtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totv=(TextView)findViewById(com.example.delhimetro.R.id.totv);
stationFrom=(Spinner)findViewById(R.id.stationFrom);
stationTo=(Spinner)findViewById(R.id.stationTo);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, all);
stationFrom.setAdapter(adapter);
stationTo.setAdapter(adapter);
stationFrom.setOnItemSelectedListener(this);
stationTo.setOnItemSelectedListener(this);
route=(Button)findViewById(R.id.route);
route.setOnClickListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch(arg1.getId()){
case R.id.stationTo:
finalTo=stationTo.getSelectedItem().toString();
totv.setText(finalTo);
break;
case R.id.stationFrom:
finalFrom=stationFrom.getSelectedItem().toString();
fromtv.setText(finalFrom);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.route:
break;
}
}
}