Android listview changing checkbox randomly

I have a list with a flag in each line, when I select any flag, and then scroll the screen, the flags for each element change evenly, I mean, to uncheck its check, and sometimes it doesn’t uncheck, and then when I scroll again, the verified one goes down, I found this Android listview question with a checkbox problem , and users say it helps them, but I can’t understand what sholud I do and help please

this is my adapter

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;

    }
}
+5
source share
2 answers

It's about looping and recycling items in a list, take a look at the concept 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

+12

getView(), , .

, ListView, ( convertView), , , View , , , View.

+2

All Articles