How to load data in Spinner Determine how they are displayed

9

I have a problem with the view of the spinners in Android, the list of data that I load in the spinner is correctly ordered from the AZ but when selecting the spinner it is shown from the Z and I have to scroll the scroll view towards up.

Could you help me to show from A down?

This is how I have my code:

        adtInsuredProfession = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item) {


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Log.e("VALORES SPINNER ", String.valueOf(position) + " " + convertView );

            View v = super.getView(position, convertView, parent);
            if (position == getCount()) {

                ((TextView) v.findViewById(android.R.id.text1)).setText("");
                ((TextView) v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
            }

            return v;
        }
    
asked by Dulce Core 11.05.2016 в 21:06
source

1 answer

5

What you comment depends on how your data is arranged or how you enter it.

I notice that within getView() you try to show the values at Spinner which is incorrect. Maybe there is a little confusion, I add an example of how to fill Spinner with values of ArrayList :

  Spinner spinner = (Spinner) findViewById(R.id.mySpinner);

    // Spinner click listener
    spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?>
                                           arg0,View arg1,int arg2,long arg3){
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    // Elementos en Spinner
    List<String> values = new ArrayList<String>();
    values.add("A");
    values.add("B");
    values.add("C");
    values.add("...");
    values.add("X");
    values.add("Y");
    values.add("Z");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);

As you can see, the values are added within an ArrayList and in the order in which they are entered they should be shown.

    
answered by 11.05.2016 в 21:44