Question

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if(v == null){
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.country_row, null);
        }

        Country item = getItem(position);

        TextView tv1 = (TextView) v.findViewById(R.id.country);
        final CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.checked);
        ctv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ctv.toggle();
                Country country = (Country) listView.getItemAtPosition(position);
                Log.d("daim", country.getCountry() + " " + country.getCode());
                //listView.getCheckItemIds();
            }
        });

        tv1.setText(item.getCountry());

        return v;
    }

When I check let's say first element (position 0), it gets checked, but when I scroll down the listview, the first position of next "section" gets also checked, this continues all the way down. For instance, I have 100 elements, and I check position 0, then position 10, 20, 30, 40 etc gets also checked. How can handle this kind of problem?

Thanks in advance.

Was it helpful?

Solution

that's because the listview recycle its view, so it keeps a pool of view that reuse. THe pattern typically I use is:

if (this row need to be checked)
        check it
else
        force uncked it
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top