0

我正在尝试从使用相同数组适配器的 2 个不同微调器获取 2 个字符串类型数据。我已经使用 switch 和 case 语句设置了 onItemselectedListener()。搜索了一段时间后,我很确定代码是正确的。使用 adapterView 变量,该函数能够确定从哪个微调器中选择了项目。但是仍然出现问题,并且在选择项目时 textView 没有改变 public class MainActivity extends Activity implements 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;
    }

}

}

4

0 回答 0