滚动时,自动检查Spinner适配器中的复选框

时间:2014-12-12 07:31:38

标签: android checkbox

我有什么:我有一个微调器,我设置了适配器的敌人,显示在下面的男女同校的适配器有checkboxtextview

发生了什么 ::当我选中一行中的复选框并向下滚动时,会自动检查以下适配器中的某些项目。当我向上滚动适配器时,我检查也保持检查

我想做什么 :: 如何在适配器中保留正确选择的复选框。当我向上和向下滚动


AdptCategories.java

public class AdptCategories extends BaseAdapter {
    ArrayList<HashMap<String, String>> categorySpinnerData;
    Context context;
    //ArrayList<ListObject> objects;

    public AdptCategories(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
        super();
        context=_context;
        categorySpinnerData=_categorySpinnerData;
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        HashMap<String, String> mapData=categorySpinnerData.get(position);
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
        }

        TextView txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);
        CheckBox categoryChkBxId = (CheckBox)convertView.findViewById(R.id.categoryChkBxId);
        if(position==0){
            txtCategoryNameId.setText(mapData.get("name"));
            categoryChkBxId.setVisibility(View.INVISIBLE);
        }else{
            txtCategoryNameId.setText(mapData.get("name"));
            txtCategoryNameId.setTag(mapData.get("id"));
            categoryChkBxId.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
}

{编辑}

public class AdptCategories extends BaseAdapter {
    ArrayList<HashMap<String, String>> categorySpinnerData;
    Context context;
    private ArrayList<Boolean> checked = new ArrayList<Boolean>();

    //ArrayList<ListObject> objects;

    public AdptCategories(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
        super();
        context=_context;
        categorySpinnerData=_categorySpinnerData;
    }

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

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

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

    static class ViewHolderItem {
        TextView txtCategoryNameId;
        static CheckBox categoryChkBxId;

    }

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

        ViewHolderItem viewHolder;
        HashMap<String, String> mapData=categorySpinnerData.get(position);

        for (int i = 0; i < this.getCount(); i++) 
        {
            checked.add(i, false);
        }

        ViewHolderItem.categoryChkBxId.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton checkbox,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked)
                {
                    checked.set(position,true);
                }
                else
                { 
                    checked.set(position,false);
                }  
            }
        });
        ViewHolderItem.categoryChkBxId.setChecked(checked.get(position));

        if(convertView==null){
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
            // well set up the ViewHolder
            viewHolder = new ViewHolderItem();
            viewHolder.txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);
            viewHolder.categoryChkBxId = (CheckBox)convertView.findViewById(R.id.categoryChkBxId);

            // store the holder with the view.
            convertView.setTag(viewHolder);
        }else{
            // we've just avoided calling findViewById() on resource every time
            // just use the viewHolder
            viewHolder = (ViewHolderItem) convertView.getTag();
        }

        if(position==0){
            viewHolder.txtCategoryNameId.setText(mapData.get("name"));
            viewHolder.categoryChkBxId.setVisibility(View.INVISIBLE);
        }else{
            viewHolder.txtCategoryNameId.setText(mapData.get("name"));
            viewHolder.txtCategoryNameId.setTag(mapData.get("id"));
            viewHolder.categoryChkBxId.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:2)

这是由于在spinner中回收视图,你可以做的是声明一个布尔数组private ArrayList<Boolean> checked = new ArrayList<Boolean>(); 并在适配器构造函数中初始化

for (int i = 0; i < this.getCount(); i++) 
 {
 checked.add(i, false);
 }

现在你的get view方法使用它如下: -

your_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox,
                boolean isChecked) {
            // TODO Auto-generated method stub
             if (isChecked)
             {
             checked.set(position,true);
             }
             else
             { 
             checked.set(position,false);
             }  
        }
    });
    your_checkbox.setChecked(your_data.get(position));

并取悦你的观众,这是一个很好的做法。