Values not showing in spinner Android

81 views Asked by At

Values not getting shown in spinner I am getting values when I click on spinner , dropdown get shown with values . But not when I select the spinner

vechType_spinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                if (position != 0)
                {

                    Toast.makeText(CreateItinerary.this, "testing vehicle", Toast.LENGTH_LONG).show();
                    selectedVehicalType = vechiclelist.get(position);

                    if (selectedVehicalType.contains("Car") || selectedVehicalType.contains("Motor Cycle") || selectedVehicalType.contains("Bike"))
                    {
                        expectedKM.setVisibility(View.VISIBLE);
                        //classSpinner.setVisibility(View.GONE);
                    }

                    else
                    {
                        expectedKM.setVisibility(View.GONE);
                    }




                }
                else
                {
                    Toast.makeText(CreateItinerary.this, "testing else vehicle", Toast.LENGTH_LONG).show();
                    expectedKM.setVisibility(View.GONE);
                    //classSpinner.setVisibility(View.GONE);

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                Toast.makeText(CreateItinerary.this, "testing NOthing selected vehicle", Toast.LENGTH_LONG).show();
            }
        });

onItemSelected is not getting called . Although the items get drop down ? Is it due to xml overlapping ??

1

There are 1 answers

0
Android Dev On

try like this in your code, make change in OnitemSelelected will work

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

                //Do What ever you want

                        if (selectedVehicalType.contains("Car") || selectedVehicalType.contains("Motor Cycle") || selectedVehicalType.contains("Bike"))
                        {
                            expectedKM.setVisibility(View.VISIBLE);
                            //classSpinner.setVisibility(View.GONE);
                        }

                        else
                        {
                            expectedKM.setVisibility(View.GONE);
                        }

            }

Add inside Oncreate

spinner = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, YourStringArray);
        adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner .setAdapter(adapter_state2);
        spinner .setOnItemSelectedListener(this);