goutham07511
goutham07511

Reputation: 11

Android Spinners Item Selection

I need a sample logic code... I have spinner in which I have 3 items ----->Biscuits, Chips, Chocolates, My Main.xml consists of only only a spinner and search button.

If i select chips from the spinner and click on the search button it should navigate to the chips page. If I Select Biscuits from the spinner and click on the search button ti should navigate to the biscuits page.

Please help me out in this issue Here is the code

public class ShoppingActivity extends Activity {
Spinner sp;
Button b;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button)findViewById(R.id.button1);
    sp=(Spinner)findViewById(R.id.spinner1);
    //sp.setOnItemSelectedListener((OnItemSelectedListener) this);

    String[] itemz={"Biscuits","Chips","Chocolates"};

    ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,itemz);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(aa);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(sp.equals("Biscuits")){
                Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
                startActivity(i);

            }
            if(sp.equals("Chips")){
                Intent j=new Intent(ShoppingActivity.this, Chips.class);
                startActivity(j);

            }
            if(sp.equals("Chocolates")){
                Intent k=new Intent(ShoppingActivity.this, Chocolates.class);
                startActivity(k);

            }
        }
    });


}

}

Upvotes: 0

Views: 607

Answers (4)

prvn
prvn

Reputation: 916

i have edited your code

b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(sp.getSelectedItem().toString().equals("Biscuits")){
            Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
            startActivity(i);

        }
        if(sp.getSelectedItem().toString().equals("Chips")){
            Intent j=new Intent(ShoppingActivity.this, Chips.class);
            startActivity(j);

        }
        if(sp.getSelectedItem().toString().equals("Chocolates")){
            Intent k=new Intent(ShoppingActivity.this, Chocolates.class);
            startActivity(k);

        }
    }
});

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93882

You can implement the method onItemSelected of your spinner.

String itemSelected = "";

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        itemSelected =(String)parent.getItemAtPosition(pos);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(itemSelected.equals("Biscuits")){
            Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
            startActivity(i);

        }
        if(itemSelected.equals("Chips")){
            Intent j=new Intent(ShoppingActivity.this, Chips.class);
            startActivity(j);

        }
        if(itemSelected.equals("Chocolates")){
            Intent k=new Intent(ShoppingActivity.this, Chocolates.class);
            startActivity(k);

        }
    }
});

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128448

Your mistake:

You are making mistake by comparting text with the spinner Object which is wrong:

if(sp.equals("Biscuits")){
    Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
    startActivity(i);
}

Solution:

Try below solution to get selected item text and implement solution as per your requirement:

Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);
String selectdItemText= mySpinner.getSelectedItem().toString();

if(selectdItemText.equals("Biscuits")){
    Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
    startActivity(i);
}

Upvotes: 2

Sanghita
Sanghita

Reputation: 1317

Try this

        if(sp.getSelectedItem().toString().equals("Biscuits")){
            Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
            startActivity(i);

        }

Upvotes: 1

Related Questions