动态微调器 - 如果选择了一个项目,则将其从其他微调器中隐藏 - onItemSelected中的getDropdownView?

时间:2014-04-09 21:44:56

标签: android dynamic spinner

所以我已经开始在Java中创建动态的微调器了,但我现在有点蠢。 我知道这是一个常见的问题,但是如何隐藏当前在微调器中选择的项目? 我已经尝试通过字符串和ArrayAdapters的ArrayList删除项目,但我注意到,一旦从列表中删除它,选择也不再引用列表项(因为它不再存在)。 我遇到了一个getDropDownView视图的改编版本,该视图隐藏/禁用了项目列表中的第一个条目,同时它仍保留在列表中(链接:http://pastebin.com/92BZvrkT)。 我现在想使用此适配器来获取所选项目的位置,然后将其从其他3个微调器中隐藏。 我有4个微调器,它们都有与它们的资源相同的ArrayList,并且每个都有自己的适配器,通过它我用来更新数组。现在,如果我想使用链接中给出的代码(更具体地说),我该怎么办:

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

在我的onItemSelectedListener中,它看起来像:

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

1 个答案:

答案 0 :(得分:0)

我有几个微调器显示相同的元素列表(或不同的列表,但共享一些元素)。在微调器中选择元素时,它将隐藏在其余微调器的下拉列表中。该解决方案与Ravi的https://stackoverflow.com/a/32351061/3031089非常相似,但适用于处理复杂对象列表(不仅仅是字符串)。我还有一个选定对象数组,并检查一个元素是否在此数组中,以确定在下拉列表中显示或隐藏它:

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
    }

}

最后,在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) {

    }

}