0

I am new to android. I am stuck with the spinner. There are two spinner item "FROM" and "TO". I want to code as..if I want to search FROM Place1, To Place2. Then after clicking button, if condition matches, it should redirect to another page. While running app, I am not getting any error, and no response comes out of clicking button.

Please see the below code. I have tried this thing by 3 way (*in comments), none of them gives desired output.

package com.aricent.aricentgroupapps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;


public class SelectLocation extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_location);


        Button route = (Button) findViewById(R.id.RouteButton);
        final Spinner spin1 = (Spinner) findViewById(R.id.fromdropdrown);
        final Spinner spin2 = (Spinner) findViewById(R.id.todropdrown);





        route.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            String str1 = spin1.getSelectedItem().toString();
            String str2 = spin2.getSelectedItem().toString();
            String plot31= "Plot 31";
            String plot314= "Plot 314";
        /*  if(String.valueOf(spin1.getSelectedItem())== "Plot 17" && String.valueOf(spin2.getSelectedItem())== "Plot 16")
            {
                Intent i = new Intent(SelectLocation.this, Route1.class);
                startActivity(i);
            }


            if (spin1.equals("Plot 17") && spin2.equals("Plot 16"))
                {
                    Intent i = new Intent(SelectLocation.this, Route1.class);
                    startActivity(i);
                }
                */

            if (str1== plot31 && str2==plot314)
            {
                Intent i = new Intent(SelectLocation.this, Route1.class);
                startActivity(i);

            }
            }
        }); 



    }

    protected String valueOf(Object selectedItem) {
        // TODO Auto-generated method stub
        return getString(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.select_location, menu);
        return true;
    }

}
4

3 回答 3

1

用这个

spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
    public void onItemSelected(AdapterView<?> main, View view, int position,
            long Id) {
            if(position==0){
            //your code
             }
             if(position==1){
            //your code
             }
    }
}
于 2013-11-13T08:19:03.910 回答
0

用这个-

if(str1.equals("plot16")&&str2.equals("plot17")){
          //do functionality
              }
于 2013-11-13T08:12:24.347 回答
0
       if (str1.equals( plot31) && str2.equals(plot314))
        {
            Intent i = new Intent(SelectLocation.this, Route1.class);
            startActivity(i);

        }

尝试这个。== 用于比较引用,而 equals() 方法用于比较值。

于 2013-11-13T08:13:50.947 回答