In a nutshell as was said by the last poster, the listview re-uses the
same objects over and over for those items on view. In your adapter you
can have an array which contains the entire content but you should be
very careful that you aren't storing to much in this array, for example
don't attempt to hold all your images in memory, chances are it will
fail at some point. Below is a "work in progress" from my application,
I've added a few more comments to make it a bit clearer whats going on:
public View getView(int position,
View convertView,
ViewGroup parent) {
View vi = convertView;
try{
// Get a reference to the actual element from the big array, this array
contains all the items that will be shown in the listview, but note the
array only stores file names, not images
imgFile objActual = m_aryImages.get(position);
// This is a temporary reference that will be used to ref to the item on
view from the list, this is not the same as objects in the array
imgFile objOnView;
if ( vi == null ) {
// If we get here then this is the first time that this object has been
created, each 'visible' item in the listview will get here.
vi = m_layoutInfater.inflate(R.layout.lvimages, null);
// Create a new object for this item, these are recycled when the
listview is scrolled.
objOnView = new imgFile();
// Set-up references to all the controls in the item
objOnView.setThumbnail((ImageView)vi.findViewById(R.id.ivthumbNail));
objOnView.setUseCheckBox((CheckBox)vi.findViewById(R.id.chkUse));
objOnView.setCCWbutton((ImageButton)vi.findViewById(R.id.btnCCW));
objOnView.setCWbutton((ImageButton)vi.findViewById(R.id.btnCW));
// Add a reference to the object
vi.setTag(objOnView);
} else {
// Get the object from the list view that we will recycle for the next view
objOnView = (imgFile)vi.getTag();
}
// In the temporary objects in the listview I store the actual LV index
so I can compare before they get recycle, helps to transfer information
back to the large array.
int intArrayRef = objOnView.getLVindex();
if ( intArrayRef != imgFile.NO_REF && intArrayRef <
m_aryImages.size() ) {
// If we get here it means the object has been used before and is about
to be recycled, so I transfer the information from the temporary object
back into the array.
imgFile objOriginal = m_aryImages.get(intArrayRef);
objOriginal.setUse(objOnView.getUse());
objOriginal.setAngle(objOnView.getAngle());
}
// Make sure the listview index is up to date for next time
objOnView.setLVindex(position);
// Get the properties of the actual array entry and populate the list
view object
String strFullPath = objActual.getFilename();
// Set-up the text view with just the file name
String[] aryFile = uwrTabActivity.splitPath(strFullPath);
if ( aryFile == null ) {
throw new Exception( "Cannot split image path!" );
}
// Transfer the data from the actual object to the object on view
objOnView.setFilename(objActual.getFilename());
objOnView.setUse(objActual.getUse());
objOnView.setAngle(objActual.getAngle());
// Set-up the use check-box
CheckBox cbUse = objOnView.getUseCheckBox();
cbUse.setText(aryFile[1]);
cbUse.setChecked(objActual.getUse());
cbUse.setTag(objOnView);
cbUse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
imgFile ii = (imgFile)v.getTag();
ii.setUse(cb.isChecked());
}
});
// Set-up rotation buttons with listeners
ImageButton btn = objOnView.getCCWbutton();
btn.setTag(objOnView);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageButton ibtn = (ImageButton)v;
imgFile ii = (imgFile)ibtn.getTag();
ii.rotateThumbnail(imgFile.ROTATE_CCW);
}
});
btn = objOnView.getCWbutton();
btn.setTag(objOnView);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageButton ibtn = (ImageButton)v;
imgFile ii = (imgFile)ibtn.getTag();
ii.rotateThumbnail(imgFile.ROTATE_CW);
}
});
// Schedule the image to be loaded in the background
objOnView.scaleAndRotateImage( true );
} catch( Exception ex ) {
Log.e( TAG, ex.getMessage() );
}
return vi;
}
}
I hope this helps...
Regards,
Simon
On 27/07/2011 1:20 PM, feiyangfeixue feiyangfeixue wrote:
Some day ago, the same problem occures to me. The reason is that the
ListView is design to reuse the ListItem, which means that the Item #9
and #17 reuse items #0-#8. So you should find some to maintain the
View status ( such as checkbox check status ) in some data structure.
For example ,you can forge a list to mark every checkbox status. In
the getView method ,when you get Checkbox view, you should set the
check status by yourself. Hopelly that can help you.
2011/7/26 Viral Brahmbhatt <[email protected]
<mailto:[email protected]>>
Hi there,
I am trying to implement a listview with image, text and checkbox
within it, using baseadapter.
the problem is, let's say i have 20 items in list, so when i
select the first item checkbox, and scroll down the list, i can
see the item #9 and #17 also get checked automatically.
can anyone pls help me to understand what is it doing?
--
*Regards,
Viral*
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:android-developers%[email protected]>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en