Search code examples
androidspinneronitemselectedlistener

how to get position or index of element in spinner


I have a spinner in my code and I want to get position of element in the array when selected. Here is my code which is running perfectly. In selection I am storing the string value of element but I also want the position count of element

public class MainActivity extends Activity implements OnItemSelectedListener {
    final Context context = this;
    private Button button;
    private String selection;
    private String[] states  = new String[]{
            "Gujrat","Jammu and Kashmir","Kerala","Karnataka","Lakshadweep","Maharashtra","Manipur","Mizoram",
            "Nagaland","New Delhi","Rajasthan","Tami Nadu","West Bengal"
    };
    ArrayList<String> categoryList = new ArrayList<String>();
    private static final String TAG = "MyActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;

        //make it fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //fix portrait orientation
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(R.layout.activity_main);
        ImageView img = (ImageView)findViewById(R.id.imageView1);
        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // custom dialog
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.menu);
                Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
                Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
                ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this,  android.R.layout.simple_spinner_item, states);
                adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spin.setAdapter(adapter_state);

                spin.setOnItemSelectedListener(MainActivity.this);
            }
        });



    }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                               long arg3) {
        // TODO Auto-generated method stub
        TextView tv = (TextView)arg1;
        selection = tv.getText().toString();
        Log.v(TAG, "index=" + selection);

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

    }

}

Solution

  • int arg2 of `onItemSelected()` is the position of selected items.
    

    So you can use it like

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                               long arg3) {
        // TODO Auto-generated method stub
    
        selection = states[arg2];
        Log.v(TAG, "index=" + arg2);
        Log.v(TAG, "selction=" + selection);
    
    }