【问题标题】:CheckBox checked on scroll automatically in ListView in androidCheckBox 在 android 的 ListView 中自动检查滚动
【发布时间】:2016-06-13 08:47:38
【问题描述】:

我是 android 新手,已经实现了一个带有复选框和 textViews 的 listView,我面临一个奇怪的选择问题。

当我选中任何一个复选框时,所有其他复选框都会自动选中,并在随机滚动后更改。

谁能帮我在我的代码中进行哪些更改来解决这个问题?

服务适配器

package one.tusk.stush.adapters;

import android.content.Context;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.company.stush.R;

import java.util.ArrayList;
import java.util.HashMap;

import one.tusk.stush.other.Const;

public class ServiceAdapter extends BaseAdapter {
    public ArrayList<HashMap<String, String>> contArray;
    SparseBooleanArray mArray;


    private Context mContext;
    String resnID, reson;
    Intent i;

    public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
        this.mContext = paramContext;
        this.contArray = contList;
        mArray = new SparseBooleanArray(contList.size());

    }

    public int getCount() {
        return this.contArray.size();
    }

    public Object getItem(int paramInt) {
        return Integer.valueOf(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    public View getView(final int paramInt, View paramView,
            ViewGroup paramViewGroup) {
        LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
                .getSystemService("layout_inflater");
        Viewholder localViewholder = null;
        Const.selectedIDs.clear();
        if (paramView == null) {
            paramView = localLayoutInflater.inflate(
                    R.layout.raw_single_contact, paramViewGroup, false);
            localViewholder = new Viewholder();

            localViewholder.tv_name = ((TextView) paramView
                    .findViewById(R.id.tv_name));
            localViewholder.chk_id = ((CheckBox) paramView
                    .findViewById(R.id.chk_id));

            paramView.setTag(localViewholder);


        } else {

            localViewholder = (Viewholder) paramView.getTag();
            localViewholder.chk_id.setOnCheckedChangeListener(null);
            localViewholder.chk_id.setChecked(mArray.get(paramInt));
        }
        localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
        localViewholder.chk_id.setChecked(mArray.get(paramInt));
        //localViewholder.chk_id.setChecked(false);
        //localViewholder.chk_id .setTag(paramInt);

        localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {


                    if (isChecked) {
                        Const.selectedIDs.add(contArray.get(paramInt));
                        Const.serviceArrayList.get(paramInt).put("flag", "1");
                    } else {
                        Const.selectedIDs.remove(contArray.get(paramInt));
                        Const.serviceArrayList.get(paramInt).put("flag", "0");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });




        return paramView;

    }

    static class Viewholder {


    TextView tv_name;
    CheckBox chk_id;


    }



}

【问题讨论】:

    标签: android listview checkbox android-arrayadapter


    【解决方案1】:

    使用SparseBooleanArray 代替ArrayList&lt;Boolean&gt;

    SparseBooleanArray mArray;
    public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
        this.mContext = paramContext;
        this.contArray = contList;
        mArray = new SparseBooleanArray(data.size());
    
    }
    

    getView()里面

    if (convertView == null) {
    
        localViewholder = new Holder();
        convertView = inflater.inflate(R.layout.item1, null);
    
        ....
        ....
    
        convertView.setTag(localViewholder );
    
    } else {
        localViewholder = (Viewholder) convertView.getTag();
        localViewholder.chk_id.setOnCheckedChangeListener(null);
        localViewholder.chk_id.setChecked(mArray.get(position));
    }
    localViewholder.chk_id.setChecked(mArray.get(position));
    localViewholder.chk_id.setOnCheckedChangeListener(new
                 OnCheckedChangeListener() {
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            mArray.put(paramInt, isChecked);
            if (isChecked) {
                Const.selectedIDs.add(isChecked);
                Const.serviceArrayList.get(paramInt).put("flag", "1");
            } else {
                Const.selectedIDs.remove(contArray.get(isChecked));
                Const.serviceArrayList.get(paramInt).put("flag", "0");
            }
        }
    });
    

    快乐编码..

    【讨论】:

    • 我已经按照你的建议编辑了我的代码,仍然没有改变..!!!请检查我编辑的代码
    • 请检查我的代码中的onChackChangeListener。并将mArray.put(paramInt, isChecked); 放入其中。
    • 代码中的“cb”是什么?什么是“chk_id”都是复选框?
    • 对不起.. cb 是 chk_id。我实际上将我的代码转换为你的。但我想我忘了在那里改变..检查我更新的答案..
    • 总是乐于助人.. :)
    【解决方案2】:

    用这个改变你的适配器类代码

     public class ServiceAdapter extends BaseAdapter {
            public ArrayList<HashMap<String, String>> contArray;
            ArrayList<Boolean> checked;
    
    
            private Context mContext;
            String resnID, reson;
            Intent i;
    
            public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
                this.mContext = paramContext;
                this.contArray = contList;
                checked = new ArrayList<>();
    
            }
    
            public int getCount() {
                return this.contArray.size();
            }
    
            public Object getItem(int paramInt) {
                return Integer.valueOf(paramInt);
            }
    
            public long getItemId(int paramInt) {
                return paramInt;
            }
    
            public View getView(final int paramInt, View paramView,
                    ViewGroup paramViewGroup) {
                LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
                        .getSystemService("layout_inflater");
                Viewholder localViewholder = null;
                Const.selectedIDs.clear();
    
                    paramView = localLayoutInflater.inflate(
                            R.layout.raw_single_contact, paramViewGroup, false);
                    localViewholder = new Viewholder();
    
                    localViewholder.tv_name = ((TextView) paramView
                            .findViewById(R.id.tv_name));
                    localViewholder.chk_id = ((CheckBox) paramView
                            .findViewById(R.id.chk_id));
    
                    paramView.setTag(localViewholder);
    
    
                localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
                localViewholder.chk_id.setChecked(false);
                localViewholder.chk_id .setTag(paramInt);
    
                localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        try {
    
    
                            if (isChecked) {
                                Const.selectedIDs.add(contArray.get(paramInt));
                                Const.serviceArrayList.get(paramInt).put("flag", "1");
                            } else {
                                Const.selectedIDs.remove(contArray.get(paramInt));
                                Const.serviceArrayList.get(paramInt).put("flag", "0");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
    
                    }
                });
    
    
    
    
                return paramView;
    
            }
    
            static class Viewholder {
    
    
            TextView tv_name;
            CheckBox chk_id;
    
    
            }
    
    
    
        }
    

    【讨论】:

    • 你好 Abhishek,我已经使用了你的适配器,但是当我滚动 listView 时,ti 会删除选择..:(
    【解决方案3】:

    尝试替换这个

    localViewholder.chk_id.setChecked(false);  
    

    到这里

    localViewholder.chk_id.setChecked(localViewholder.chk_id.isChecked());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多