使用 listiview 取消选中 Onscroll 选中的复选框

Posted

技术标签:

【中文标题】使用 listiview 取消选中 Onscroll 选中的复选框【英文标题】:Onscroll selected checkbox getting unchecked using listiview 【发布时间】:2016-06-03 20:04:14 【问题描述】:

在我的应用程序中,我添加了checkboxListView,并且我还限制了checkbox,用户不能选择超过5 个checkbox,但问题在于滚动我选择的checkbox 未选中以下是我的sn-p代码,有人可以帮我吗

public class CustomAdapter extends BaseAdapter 

    private LayoutInflater inflater = null;
    Context context;

    String rup = "\u20B9";
    private ArrayList<ModelPooja> listData;
    boolean checked[];

    public CustomAdapterPooja(Context mainActivity, ArrayList<ModelPooja> listData) 
        // TODO Auto-generated constructor stub

        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        checked = new boolean[listData.size()];
        for (int i = 0; i < checked.length; i++) 
            checked[i] = listData.get(i).isselected;
        

    

    @Override
    public int getCount() 
        // TODO Auto-generated method stub
        return listData.size();
    

    @Override
    public Object getItem(int position) 
        // TODO Auto-generated method stub
        return position;
    

    @Override
    public long getItemId(int position) 
        // TODO Auto-generated method stub
        return position;
    


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        // TODO Auto-generated method stub
         final ViewHolder holder;

       if (convertView == null) 
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
        holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
        holder.serviceprice = (TextView) convertView.findViewById(R.id.list_item_poojaprice);
        holder.dayss = (TextView) convertView.findViewById(R.id.list_item_poojadays);
        holder.txtseledates = (TextView) convertView.findViewById(R.id.selecteddatess);
        holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);

       convertView.setTag(holder);

     else 
           holder = (ViewHolder) convertView.getTag();
       
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) 

                if (checkMaxLimit()) 

                    if (listData.get(position).isselected && b) 
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                     else 
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();

                    
                 else 
                    if (b) 
                        listData.get(position).isselected = true;
                     else 
                        listData.get(position).isselected = false;
                    
                

            
        );
        if (listData.get(position).isselected()) 
            holder.checks.setChecked(true);
         else 
            holder.checks.setChecked(false);
        

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        holder.dayss.setText(listData.get(position).getPOOJA_LISTING_DAYS());
        holder.serviceprice.setText(rup + listData.get(position).getPOOJA_LISTING_AMOUNT());

        return convertView;
    

    public boolean checkMaxLimit() 
        int countermax = 0;
        for (int i = 0; i < checked.length; i++) 
            checked[i] = false;
            checked[i] = listData.get(i).isselected();
            if (listData.get(i).isselected()) 
                countermax++;
            
        
        return countermax >= 5 ? true : false;
    

    public class ViewHolder 
        TextView tv;
        TextView serviceprice;
        public CheckBox checks;
        public TextView dayss;
        public TextView txtseledates;
    



【问题讨论】:

dj-android.blogspot.in/2013/02/… 【参考方案1】:

问题出在您的 getView() 方法中。您设置 OnCheckedChangeListener ,然后将复选框设置为 true 或 false ,这会在该侦听器中触发回调。您应该先设置复选框检查状态,然后再设置 OnCheckedChangeListener

另外,那个 boolean checked[] 字段没用,所以我稍微简化了你的代码:

 public class CustomAdapter extends BaseAdapter 
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) 
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    @Override
    public int getCount() 
        return listData.size();
    

    @Override
    public Object getItem(int position) 
        return listData.get(position);
    

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        final ViewHolder holder;

        if (convertView == null) 
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        else 
            holder = (ViewHolder) convertView.getTag();
        
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) 
            holder.checks.setChecked(true);
         else 
            holder.checks.setChecked(false);
        

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) 

                if (checkMaxLimit()) 

                    if (listData.get(position).isselected && b) 
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                     else 
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    
                 else 
                    if (b) 
                        listData.get(position).isselected = true;
                     else 
                        listData.get(position).isselected = false;
                    
                
            
        );

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    

    public boolean checkMaxLimit() 
        int countermax = 0;
        for(ModelPooja item : listData)
            if(item.isselected)
                countermax++;
            
        
        return countermax >= 5;
    

    public class ViewHolder 
        TextView tv;
        public CheckBox checks;
    

【讨论】:

你能说一件事吗??【参考方案2】:

在适配器中创建一个初始值为假的布尔数组。如果用户检查该数组,则使数组的位置为真。取消选中使其为假。始终从该布尔数组中设置复选框值。

【讨论】:

【参考方案3】:

这是因为当您从手机的上侧或下侧滚动列表视图时,列表视图的所有项目都会重新生成并设置为默认值。 为了避免这种情况,请创建一个模型类来保存复选框的当前值(true 或 false),并通过从该模型实例调用 getter 方法在适配器中设置复选框的值。 例如:

模型.java

public class AddressModel 

    private boolean isChecked;
    private String address;
    private int alAddressDelete;

    public AddressModel()

    
    public AddressModel(boolean isChecked, String address, int alAddressDelete) 
        this.isChecked = isChecked;
        this.address = address;
        this.alAddressDelete = alAddressDelete;
    

    public boolean getIsChecked() 
        return isChecked;
    

    public void setIsChecked(boolean isChecked) 
        this.isChecked = isChecked;
    

在您的适配器类中,通过在setOnCheckedChangeListener() 中调用setIsChecked(boolean isChecked) 来设置复选框的值。 调用 getIsChecked() 来设置复选框的值。

【讨论】:

【参考方案4】:

先调用setOnCheckedChangeListener(),再调用setChecked(),否则setChecked()会触发前一个ViewHolder对象的setOnCheckedChangeListener()

【讨论】:

以上是关于使用 listiview 取消选中 Onscroll 选中的复选框的主要内容,如果未能解决你的问题,请参考以下文章

带有图像选择和取消选择的 Horizo​​ntalListview

如何使用 jQuery 使用按钮选中/取消选中所有复选框?

使用 jquery 选中或取消选中 parent parents 复选框

如果已经使用 Jquery 选中,需要帮助取消选中单选按钮

使用 Jquery 选中/取消选中输入框 [重复]

javascript [jQuery单选框选中与取消选中应用]记录jQuery使用过程中单选框选中与取消选中的坑。 #JS #jQuery #input #radio