Spinner with custom adapter show the selected item Android

hasmet picture hasmet · Jul 16, 2013 · Viewed 9.3k times · Source

in my program use a spinner with a custom adapter. The adapter is:

private class lenguageSpinnerAdapter extends BaseAdapter implements
        SpinnerAdapter {

    private ArrayList<String> lenguages;

    public lenguageSpinnerAdapter(Context context,
            ArrayList<String> lenguages) {
        this.lenguages = lenguages;
    }

    public int getCount() {
        return lenguages.size();
    }

    public Object getItem(int position) {
        return lenguages.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context
                    .getApplicationContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lenguage_item_layout,
                    parent, false);
        }

        TextView lenguage = (TextView) convertView
                .findViewById(R.id.textViewLenguage);
        lenguage.setText(lenguages.get(position).toString());


        return convertView;
    }

}

And I use this adapter in an Activity:

ArrayList<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("IT");
    spinnerArray.add("EN");
    spinnerArray.add("PR");

    final lenguageSpinnerAdapter lenguageAdapter = new lenguageSpinnerAdapter(
            getContext(), spinnerArray);
    spinnerLenguage.setAdapter(lenguageAdapter);

The problem is that this spinner show always the three lenguages, but I would like him to show only the not selected lenguages (EN and PR if IT il selected, or IT and PR if EN in selected). Any ideas?

Answer

mromer picture mromer · Jul 16, 2013
    spinnerLenguage.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            // Build your new array
            // Build your new adapter
            // Set your new adapter to spinnerLenguage

        }

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

        }
    });