2011-10-15 30 views
0
@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; 
    } 

当我检查让我们说第一个元素(位置0)时,它会检查,但是当我向下滚动列表视图,下一个“节”的第一位置被也查了,这样下去一直往下。例如,我有100个元素,我检查位置0,然后位置10,20,30,40等也被检查。如何处理这种问题?的Android - CheckedTextView检查错误的元素

在此先感谢。

回答

2

这是因为listview回收它的视图,所以它保留了一个重用的视图池。通常我使用的模式是:

if (this row need to be checked) 
     check it 
else 
     force uncked it