【问题标题】:android listview , the checkbox change randomandroid listview,复选框随机变化
【发布时间】:2013-01-06 06:59:13
【问题描述】:

我在每一行都有一个带有复选框的列表视图,当我选择任何复选框然后滚动屏幕时,每个项目的复选框都会随机更改,我的意思是取消选中它成为选中状态,有时它保持未选中状态,然后当我滚动时再次向上,选中的变为取消选中,我发现了这个问题Android listview with checkbox problem,用户说这对他们有帮助,但我不明白我应该做什么,请帮忙

这是我的适配器

class RestaurantAdapter extends BaseAdapter {
    private static LayoutInflater inflater = null;
    private ArrayList<HashMap<String, String>> data;
    Activity activity;

    public RestaurantAdapter(Activity activity,
            ArrayList<HashMap<String, String>> data) {
        // TODO Auto-generated constructor stub
        this.activity = activity;
        this.data = data;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.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(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.restaurant_multi_select_list_item,
                    null);
        TextView name = (TextView) vi
                .findViewById(R.id.restaurant_multi_select_title);
        ImageView image = (ImageView) vi
                .findViewById(R.id.restaurant_multi_select_list_item_image);
        CheckBox cb = (CheckBox) vi
                .findViewById(R.id.restaurant_multi_select_checkBox);
        HashMap<String, String> restaurant = data.get(position);
        name.setText(restaurant.get("name"));

        image.setImageResource(Integer.parseInt(restaurant.get("image")));
        return vi;

    }
}

【问题讨论】:

    标签: android android-listview android-checkbox


    【解决方案1】:

    它是关于循环和循环列表视图中的元素,看看converView 的概念。

    我编辑了你的代码并添加了一个状态数组来保存每个复选框的最后一个值并在每个循环中加载它并循环使用,

    class RestaurantAdapter extends BaseAdapter {
        private ArrayList<Boolean> status = new ArrayList<Boolean>();
        private static LayoutInflater inflater = null;
        private ArrayList<HashMap<String, String>> data;
        Activity activity;
    
        public RestaurantAdapter(Activity activity,
                ArrayList<HashMap<String, String>> data) {
            // TODO Auto-generated constructor stub
            this.activity = activity;
            this.data = data;
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 0; i < data.size(); i++) {
                status.add(false);
            }
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.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
            View vi = convertView;
            if (vi == null)
                vi = inflater.inflate(R.layout.restaurant_multi_select_list_item,
                        null);
            TextView name = (TextView) vi
                    .findViewById(R.id.restaurant_multi_select_title);
            ImageView image = (ImageView) vi
                    .findViewById(R.id.restaurant_multi_select_list_item_image);
            CheckBox cb = (CheckBox) vi
                    .findViewById(R.id.restaurant_multi_select_checkBox);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        status.set(position, true);
                    } else {
                        status.set(position, false);
                    }
                }
            });
            cb.setChecked(status.get(position));
            HashMap<String, String> restaurant = data.get(position);
            name.setText(restaurant.get("name"));
    
            image.setImageResource(Integer.parseInt(restaurant.get("image")));
            return vi;
    
        }
    }
    

    不是你必须在 getview 上使用 setOnCheckedChangeListener ,而不是在 listview 上

    【讨论】:

      【解决方案2】:

      您需要在 getView() 中明确设置复选框的状态,具体取决于是否应检查该特定行。

      为表示 ListView 元素而创建的视图通常会在滚动时出于性能原因被回收(这是您在代码中使用 convertView 所做的事情),因此您未明确重新设置的任何视图属性当您重用该 View 时,将只保留上次使用该特定 View 实例时的任何状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-06
        • 1970-01-01
        • 1970-01-01
        • 2018-10-03
        • 2011-07-31
        • 2016-02-03
        • 2015-06-07
        • 1970-01-01
        相关资源
        最近更新 更多