Values not showing in spinner Android

时间:2015-06-26 09:52:14

标签: android android-spinner

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 个答案:

答案 0 :(得分:0)

在您的代码中尝试这样,在OnitemSelelected中进行更改将起作用

      @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);
                        }

            }

在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);