【问题标题】:Checkbox in Spinner adapter is automatically checked when I scroll it当我滚动它时,微调器适配器中的复选框会自动选中
【发布时间】:2014-12-12 07:31:38
【问题描述】:

我有什么:我有一个微调器,我已经设置了适配器,下面以 coed 显示的适配器有一个 checkbox 和一个 textview

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

我想做的事:: 如何始终在适配器中保留正确选择的复选框。当我上下滚动时


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

【问题讨论】:

  • 使用ViewHolder
  • AdapterView 重用滚动视图,以便在数组的帮助下更好地跟踪选中的项目,并在 getView 方法中选中和取消选中复选框
  • @Triode 我面临同样的问题。我使用自定义适配器来填充我的列表视图行。我做了我的列表视图多项选择。当我滚动列表视图时,我的支票消失了。我该如何解决这种情况?我应该为选定的项目使用新数组吗? stackoverflow.com/questions/28714738/…

标签: android checkbox


【解决方案1】:

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

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

请使用viewholder,这是个好习惯。

【讨论】:

  • 你的分析器中的your_data是什么,你指的是ArrayList&lt;Boolean&gt; checked = new ArrayList&lt;Boolean&gt;();
  • 它是您传递的数组列表以填充微调器
  • ViewHolderItem.categoryChkBxId.setChecked(categorySpinnerData.get(position)); ......我得到了The method setChecked(boolean) in the type CompoundButton is not applicable for the arguments (HashMap&lt;String,String&gt;) 的错误......我也被要求将getView 定位为最终...这不是问题吗?
  • 对不起,我很着急,您的数据已检查,是的,您可以将位置设为最终位置,这不会造成任何问题,您已经实现了视图模式,它将在实现适配器时始终提高效率和速度使用 viewholder 模式是个好习惯
  • 谢谢,请检查编辑....我在ViewHolderItem.categoryChkBxId.setOnCheckedChangeListener有一个空指针异常
猜你喜欢
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多