在Spinner中将项目用作提示(默认项目)并将其隐藏在下拉菜单中 [英] Use an item as hint in Spinner (default item) and hide it in the dropdown

查看:253
本文介绍了在Spinner中将项目用作提示(默认项目)并将其隐藏在下拉菜单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码选择列表的最后一项作为微调框的提示(即,微调框中的默认选定项),并试图从下拉菜单中将其隐藏.

I am using the below code to select the last item of my list as hint of the spinner (ie. the default selected item in the spinner) and am trying to hide it from the dropdown menu.

List<String> rfpType = new ArrayList<>();
rfpType.add("Job");
rpType.add("Talent");  
rfpType.add("Vendor");
rfpType.add("Sponsor");
rfpType.add("RFP Title");

HintAdapter dataAdapter1 = new HintAdapter(getActivity(), android.R.layout.simple_list_item_1, rfpType);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerRFPType.setAdapter(dataAdapter1);
spinnerRFPType.setSelection(dataAdapter1.getCount());

HintAdapter

class HintAdapter extends ArrayAdapter<String>{

    public HintAdapter(Context context, int theLayoutResID , List<String> list){
        super(context, theLayoutResID, list);
    }

    @Override
    public int getCount() {
        // don't display last item. It is used as hint.
        int count = super.getCount();
        return count > 0 ? count-1 : count;
    }

}

但是它将倒数第二项显示为默认值.并隐藏我要用作提示的最后一项.建议我一个正确的解决方案.

But it displays the second last item as default. And hides the last item which I want to use as the hint. Suggest me a correct solution.

推荐答案

您需要在适配器类中实现以下方法:

You need to implement below method in your adapter class:

它将为您提供帮助:

对于getCount返回计数也不要减少

 @Override
 public View getDropDownView(int position, View convertView,
        ViewGroup parent) 
 {
    LayoutInflater inflater = getLayoutInflater(null);
    convertView = inflater.inflate(theLayoutResID, parent,
            false);
    convertView= null;

    if(position == list.size() - 1)
    {
        holder.textView.setVisibility(View.GONE);
        convertView= holder;
    }
    else{
         convertView= super.getDropDownView(position, null, parent);
    }
    return convertView;

}

这篇关于在Spinner中将项目用作提示(默认项目)并将其隐藏在下拉菜单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆