如何避免自定义微调器Android中的默认文本问题?

时间:2016-01-13 08:05:46

标签: android spinner adapter

我已经使用自定义适配器完成了自定义微调器,但是每次都获取一些默认文本如何避免此问题每次只应在微调器上显示第0个位置文本:

CourseAdapter courseAdapter = new CourseAdapter(mContext);
customViewHolder.spinner.setAdapter(courseAdapter);

而CourseAdapter.java

public class CourseAdapter extends BaseAdapter {

  public static final String TAG=CourseAdapter.class.getName();
    List<String> courses =new ArrayList<>(); // code to get the courses ArrayList
    Context context;
    private void fillCourses()
    {
        for(int i =0;i<10;i++)
        {
           if(i == 0)
           {
               courses.add("Choose");
           }
            else
           {
               courses.add("courses:"+i);
           }
        }
    }
    public CourseAdapter(Context context)
    {
        this.context = context;
        fillCourses();
    }

    @Override
    public int getCount() {
        return courses.size();
    }

    @Override
    public Object getItem(int position) {
        return courses.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderFilterItem viewHolder;
        if (convertView == null) {
            Log.d(TAG, "fillCourses size:" + courses.size());
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.layout_customspinner, parent, false);
            viewHolder = new ViewHolderFilterItem();
            viewHolder.textView = (TextView) convertView.findViewById(R.id.txtViewFilterItem);
            viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkboxFilterItem);
            //store the holder with the view.
            convertView.setTag(viewHolder);
            return convertView;
        }
        else
        {
            Log.d(TAG, "convertview != null:" + courses.size());
            viewHolder = (ViewHolderFilterItem) convertView.getTag();
        }
        if(courses != null && courses.size() > 0) {
            String itemText = courses.get(position);
            if (itemText != null) {
             viewHolder.textView.setText(itemText);
             viewHolder.checkBox.setChecked(true);
            }
            if(position == 0)
            {
             viewHolder.checkBox.setVisibility(View.GONE);
            }
        }
        return convertView;
    }
    private class ViewHolderFilterItem{
        TextView textView;
        CheckBox checkBox;}

}[![[why Filter item text with checkbox appearing ?][1]][1]

1 个答案:

答案 0 :(得分:0)

设置courseAdapter.notifyDataSetChanged()

后,您必须使用customViewHolder.spinner.setAdapter(courseAdapter);

希望这有帮助。