smartmouse
smartmouse

Reputation: 14404

How to start activity on item selected?

I created a drop-down menu and i overrided the onItemSelected method:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();

    Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();
}

With the above code if i select an item then it shows a toast with selected item. I tried to edit the code in this way:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();

    Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();

    Intent intent = new Intent(Selection.this, MainActivity.class);
    startActivity(intent);
}

Why it doesn't allow me to select an item and go to MainActivity instead?

Upvotes: 0

Views: 931

Answers (1)

said
said

Reputation: 106

slm;according to the docs :

"Spinners provide a quick way to select one value from a set.In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one."

so it's normal to trigger the callback on the first item shown in your list and implement

it;;you can handle that by ignoring the first callback and put the first item shown in two

different positions.OR see more in this link " How to keep onItemSelected from firing off on a newly instantiated Spinner? ".

Upvotes: 2

Related Questions