Search code examples
androidpositionspinnerandroid-spinner

Android Spinner duplicate item gives only position of the first duplicated item


I made a spinner with items from MySQL. In MySQL I have got 2 items with the same value. In the spinner I have got 2 duplicates then. When I try to select the second one I get the position of the first one (this is not correct), and when I select the first one I get the position of the first one (this is correct).

Code:

sp.setVisibility(View.VISIBLE);

    //BIND
    final ArrayAdapter adapter = new ArrayAdapter(c, android.R.layout.simple_spinner_dropdown_item, optochtenarray){
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View itemView =  super.getDropDownView(position, convertView, parent);

            if (position == mSelectedIndex) {
                itemView.setBackgroundColor(Color.rgb(56,184,226));
            }
            else {
                itemView.setBackgroundColor(Color.TRANSPARENT);
            }

            return itemView;
        }
    };
    sp.setAdapter(adapter);

    //call ID
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
            String selectedItem = sp.getSelectedItem().toString();

            if (selectedItem != "Kies..."){
                //kleur veranderen
                mSelectedIndex = sp.getSelectedItemPosition();
                Log.e("selectPosition", Integer.toString(mSelectedIndex));

                Log.e("ID", idarray.get(position));
                //naar de volgende pagina met de 'ID'
                Intent myIntent = new Intent(c, gekozenOptocht.class);
                myIntent.putExtra("ID", idarray.get(position)); //Optional parameters
                c.startActivity(myIntent);
                sp.setSelection(adapter.getPosition("Kies..."));
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

Why am I getting the position of the first one when I try to select the second one?


Solution

  • PROBLEM SOLVED! It has something to do with the custom spinner that I had found on the internet.