Spinner在android中隐藏了下拉视图的第一个项目

时间:2014-06-16 10:29:01

标签: android show-hide android-spinner

  

我有一个微调器,我有自定义适配器来添加图像和文本,当我点击微调器时,他应隐藏第一项的数组。   我试过下面的代码,它的工作第一项已经不见了,但是再生项目有隐形模式,如何解决这个问题

这是我的代码

private String[] accident_type_content = { "Type Of emergency","Road Accident", "Medical Emergency", "Crime", "Poisoning", "Fire","Drowning" };
private int accident_type_content_images[] = {R.drawable.ic_type_of_emergency, R.drawable.ic_road_accident,R.drawable.ic_medical_emergency, R.drawable.ic_crime_rape,R.drawable.ic_posion, R.drawable.ic_fire, R.drawable.ic_other };
  

我的数组适配器类

public class AccidentType extends ArrayAdapter<String> {

    Holder holder = new Holder();

    public AccidentType(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
    }

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

        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.icon = (ImageView) convertView.findViewById(R.id.image);

        if(position==0)
        {
            holder.icon.setVisibility(View.GONE);
            holder.icon.setVisibility(View.GONE);
        }
        else{
            holder.icon.setVisibility(View.VISIBLE);
            holder.icon.setVisibility(View.VISIBLE);
        }
        return convertView;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView,
            ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater(null);
        View row = inflater.inflate(R.layout.no_accident, parent,
                false);

        holder.name = (TextView) row.findViewById(R.id.name);
        holder.icon = (ImageView) row.findViewById(R.id.image);
        holder.name.setText(accident_type_content[position]);

        holder.icon.setVisibility(View.VISIBLE);
        holder.icon.setVisibility(View.VISIBLE);

        if(position==accident_type.getSelectedItemPosition())
        {
            row.setBackgroundColor(getContext().getResources().getColor(R.color.spinner_selected_bg));
        }


        holder.icon.setImageResource(accident_type_content_images[position]);

        return row;
    }
    private class Holder
    {
        TextView name;
        ImageView icon;
    }

}

并添加到微调器

accident_type.setAdapter(new AccidentType(getActivity(),R.layout.accident_content, accident_type_content));

  

我想在微调器上隐藏“紧急类型”和ic_type_of_emergency图像

1 个答案:

答案 0 :(得分:1)

设置要隐藏的索引的索引(或数组列表)。

public class AccidentType extends ArrayAdapter<String> {

    Holder holder = new Holder();
    private int hideItemIndex;

    public AccidentType(Context context, int textViewResourceId,
            String[] objects, int hideItemIndex) {
        super(context, textViewResourceId, objects);
        this.hidingItemIndex = hidingItemIndex;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater(null);
        convertView = inflater.inflate(R.layout.no_accident, parent,
                false);
        convertView= null;
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.icon = (ImageView) convertView.findViewById(R.id.image);

        if(position==hideItemIndex)
        {
            holder.icon.setVisibility(View.GONE);
            holder.icon.setVisibility(View.GONE);
            convertView= holder;
        }
        else{
             convertView= super.getDropDownView(position, null, parent);
        }
        return convertView;

    }

创建项目列表。

int hideItemIndex = 0;

CustomAdapter dataAdapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, list, hideItemIndex);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);

我认为这可能会对你有所帮助。