5

我在每一行都有一个带有复选框的列表视图,当我选择任何复选框然后滚动屏幕时,每个项目的复选框都会随机更改,我的意思是取消选中它变成选中,有时它保持取消选中,然后当我再次向上滚动时,选中的变成取消选中,我发现这个问题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;

    }
}
4

2 回答 2

12

它是关于循环和循环列表视图中的元素,看看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;

    }
}

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

于 2013-01-22T22:18:38.137 回答
2

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

为表示 ListView 元素而创建的视图通常会在滚动时出于性能原因(这是您在代码中convertView使用View 将简单地保留它们在最后一次使用该特定 View 实例时的任何状态。

于 2013-01-22T22:14:42.483 回答