toast is not showing in My second spinner

156 views Asked by At

I have to make two dropdown list so added two spinners in code.when I have selected first spinner toast shows selected item but after selecting second spinner toast message is not showing .Please help.

package listdisplay.example.com.spinnertest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import static android.R.attr.country;
import static android.widget.Toast.makeText;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    TextView tv;
    Spinner spinner;
    Spinner spinner2;
    String[] country = {"India","China","Pakistan","America"};
    String[] city = {"Mumbai","Delhi","Noida","Vashi"};
    String[] countrydummy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView);
        spinner = (Spinner)findViewById(R.id.spinner);
        spinner2=(Spinner)findViewById(R.id.spinner2);
        spinner.setOnItemSelectedListener(this);

        ArrayAdapter<String> countryad=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,country);
        spinner.setAdapter(countryad);
        countrydummy=country;

        ArrayAdapter<String> cityad=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,city);
        spinner2.setAdapter(cityad);

    }



    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {  // i for position and l for id
        Spinner spin1= (Spinner)parent;
        Spinner spin2= (Spinner)parent;
//First spinner selection
        if(spin1.getId() == R.id.spinner)
        {
            String item = parent.getItemAtPosition(i).toString();
            Toast t1=makeText(getApplicationContext(),"item selected : "+item,Toast.LENGTH_SHORT);
            t1.show();



        }
//second spinner selection
        if(spin2.getId() == R.id.spinner2)
        {
            String item = parent.getItemAtPosition(i).toString();
            Toast t2=makeText(getApplicationContext(),"item selected : "+item,Toast.LENGTH_SHORT);
            t2.setGravity(Gravity.CENTER, 0, 0);
            t2.show();

        }



    }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
1

There are 1 answers

1
hemantv On

You haven't actually set any listener for the second spinner. Set a listener for it the same way you did for the first spinner.

spinner2.setOnItemSelectedListener(this);