2

So I've gotten around creating dynamic spinners in Java, but I am in a bit of a pickle now. I know this is a common question, but how does one hide an item that is currently selected in the spinner? I've tried removing the items via ArrayList of strings and ArrayAdapters, but I've noticed as soon as it's removed from the list, the selection is no longer referenced to the list item either (because it does not exist anymore). I've come across an adaptation of the getDropDownView view that hides/disables the first entry from the list of items, while it is still kept in the list (link: http://pastebin.com/92BZvrkT). I would now like to use this adapter to fetch the position of the selected item and then hide it from 3 other spinners. I've got 4 spinners, they all have the same ArrayList as their resource and each one of them has its own adapter via which I used to update the array. Now, what can I do if I want to use this code given in the link (more specifically):

public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View v = null;

        // If this is the initial dummy entry, make it hidden
        if (position == 0) {
            TextView tv = new TextView(getContext());
            tv.setHeight(0);
            tv.setVisibility(View.GONE);
            v = tv;
        }
        else {
            // Pass convertView as null to prevent reuse of special case views
            v = super.getDropDownView(position, null, parent);
        }

        // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
        parent.setVerticalScrollBarEnabled(false);
        return v;
    }
};

in my onItemSelectedListener, which will look like:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    boolean countertest = false;
    switch(parent.getId())
    {
         case R.id.spinner_1:
            //there used to be the removal from the list and adding to it, until I've noticed my fatal mistake (removal of selected item)
            //I want to add the upper method, getDropDownView here somewhere, so I can control which item gets hidden
            break;
         case R.id.spinner_2:
            //... same here
            break;
    }
alkra_dev
  • 97
  • 2
  • 11

1 Answers1

0

I have several spinners displaying the same list of elements (or different lists but sharing some elements). When an element is selected in a spinner, it will be hidden at the dropdown lists of the rest of spinners. The solution is very similar to Ravi's https://stackoverflow.com/a/32351061/3031089, but adapted to handle lists of complex objects (not only a string). I also have an array of selected objects and check if an element is in this array to determine to show or hide it in the dropdownlist:

public class MyAdapter extends ArrayAdapter<MyObject> {

    private Context mContext;
    private int resId;

    MyAdapter(Context context, int resource, List<MyObject> objects) {
        super(context, resource, objects);
        mContext = context;
        resId = resource;
    }

    private class MyHolder {
        TextView myTextView;
        ImageView myImageView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        MyObject myObject = getItem(position);
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(resId, parent, false);
            myHolder = new MyHolder();
            myHolder.myTextView = (TextView) convertView.findViewById(R.id.myTextView);
            myHolder.myImageView = (ImageView) convertView.findViewById(R.id.myImageView);
            convertView.setTag(myHolder);
        }
        else
            myHolder = (MyHolder) convertView.getTag();
        myHolder.myTextView.setText(myObject.name);
        myHolder.myImageView.setImageBitmap(myObject.picture);
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        MyObject myObject = getItem(position);
        if (mustHide(myObject))
            return new View(parent.getContext());
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(resId, parent, false);
            myHolder = new MyHolder();
            myHolder.myTextView = (TextView) convertView.findViewById(R.id.myTextView);
            myHolder.myImageView = (ImageView) convertView.findViewById(R.id.myImageView);
            convertView.setTag(myHolder);
        }
        else
            myHolder = (MyHolder) convertView.getTag();
    // The next if loop is necessary because previously hidden elements have null tag
        if (myHolder == null) {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(resId, parent, false);
            myHolder = new MyHolder();
            myHolder.myTextView = (TextView) convertView.findViewById(R.id.myTextView);
            myHolder.myImageView = (ImageView) convertView.findViewById(R.id.myImageView);
            convertView.setTag(myHolder);
        }
        myHolder.myTextView.setText(myObject.name);
        myHolder.myImageView.setImageBitmap(myObject.picture);
        return convertView;
    }

    private boolean mustHide(MyObject myObject) {
        // Check if an object is in the array of selected items
        // If so, return true; else return false
    }

}

Finally, update your array of selected objects at the OnItemSelectedListener:

public class IngItemSelectedListener implements AdapterView.OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // Update your list of selected objects 
    // and call notifyDataSetChanged() at all your spinners
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}
Community
  • 1
  • 1
MJim
  • 39
  • 5