-1
public class MainActivity extends Activity implements
    AdapterView.OnItemSelectedListener {

String animalList[] = {"Lion", "Tiger", "Monkey", "Elephant", "Dog", "Cat", "Camel"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);
    spin.setOnItemSelectedListener(this);


    ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spin.setAdapter(aa);
}


//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

    switch (position){
        case 0:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Lion.class));
            break;

        case 1:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Tiger.class));
            break;

    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

I created a spinner. when i run the app its automatically jumps to first activity (Lion.class). what i want is, after Lion be selected on spinner, it goes to that activity. how can I fix it?

i read lots of tutorials but i cant find the answer. i know it must be easy

2 Answers2

0

Add this in onCreate()..so spinner will not automatically move to new activity in first selection.

spin.setAdapter(aa);
spin.setSelection(0, false); //add this

Now in onItemSelected() add:

 switch (position){
        case 0:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Lion.class));
            break;

        case 1:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Tiger.class));
            spin.setSelection(0, true);  //add this
            break;
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

you can add select item as first option as this code

public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {

String animalList[] = {"Select Item","Lion", "Tiger", "Monkey", "Elephant", "Dog", "Cat", "Camel"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);
spin.setOnItemSelectedListener(this);


ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(aa);
}


//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

switch (position){
    case 1:
        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Lion.class));
        break;

    case 2:
        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Tiger.class));
        break;

}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

or add boolean flag like this code below

    public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
boolean flag = false;
String animalList[] = {"Lion", "Tiger", "Monkey", "Elephant", "Dog", "Cat", "Camel"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);
spin.setOnItemSelectedListener(this);


ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(aa);
}


//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

switch (position){
    case 0:
    if(flag){

        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Lion.class));
    }
           break;

    case 1:
        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Tiger.class));
        break;

}
flag = true; 
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

becase when activity start will set a spinner in the first pstion and triger onItemSelected so using flag you can avoid frist triger

3zcs
  • 35
  • 4
  • 18
  • thanks. I guess i didn't say my mean correctly. i have welcoming screen with spinner on top. i want after a client(user) selects an item on spinner then we go to that activity. but right now when we open app, it skips welcoming screen and it goes to the first activity on spinner. i want to solve this. thanks for your help. – Adel Rafiee Apr 05 '17 at 08:29
  • I understand you and both codes above are work correctly, try it just replace your code with this snippet it will work – 3zcs Apr 05 '17 at 08:32
  • when spinner moves to first, will hit your onItemSelected, so in the first time just skip it, there are many ways to do that. – 3zcs Apr 05 '17 at 08:35
  • yeah the codes work properly. but for example by using flag, when we open app, it goes to second activity on spinner – Adel Rafiee Apr 05 '17 at 13:45
  • i want non of them be selected until the user choose one of them, and after selection we go from welcoming screen to that activity – Adel Rafiee Apr 05 '17 at 13:47
  • just use flag over switch of position – 3zcs Apr 05 '17 at 14:02