Reputation: 1026
I have a problem with ListView. the thing is that when I click on any item from list, all elements are changing their positions. I've made ListViewAdapter that extends BaseAdapter and in getView I take data from public static ArrayList of items which is populated by AsyncTask with ProgressBar (which is called at the end of onCreate() ). The ListView takes new adapter only once. I have made neither notifyDataSetChanged() method on adapter nor any kind of invalidate(). ArrayList of items isn't changed from code anywhere. Thanks to debugger I know that items are relocating after finishing onItemClick() method (which has only three lines: declare new Intent, put extras and startActivity() ) from ListView, but before first line from the new Activity's onCreate(). I don't know where to search for the reason of that issue. Any suggestions? What kind of source code will help here?
Here's getView():
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.trophies_listitem, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.TrophiesListItemName);
holder.text2 = (TextView) convertView.findViewById(R.id.TrophiesListItemSpecies);
holder.text3 = (TextView) convertView.findViewById(R.id.TrophiesListItemWeigth);
holder.text4 = (TextView) convertView.findViewById(R.id.TrophiesListItemJagdrevier);
holder.photo = (ImageView) convertView.findViewById(R.id.TrophiesListItemPhoto);
holder.label1 = (TextView) convertView.findViewById(R.id.TrophiesListLabel1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
return convertView;
}
DownloadedTrophy t = (DownloadedTrophy) trophies.get(position);
holder.text.setText(t.getTrophy_title());
if(t.getTrophy_species()!=null && t.getTrophy_species().length()>0 && Integer.parseInt(t.getTrophy_species())>0)
holder.text2.setText(Main.mSpecies.getItemByKeyValue("id", String.valueOf(Integer.valueOf(t.getTrophy_species()) - 1)).get("name"));
holder.text3.setText(t.getTrophy_weight());
holder.text4.setText(t.getTrophy_place());
byte [] picture = t.getPictureSmall();
if(picture!=null){
Bitmap bm = BitmapFactory.decodeByteArray(picture, 0, picture.length);
holder.photo.setImageBitmap(bm);
} else {
Bitmap bm;
bm = BitmapFactory.decodeResource(res,R.drawable.trophy_none);
holder.photo.setImageBitmap(bm);
}
holder.label1.setText("Ort:");
return convertView;
}
Upvotes: 0
Views: 439
Reputation: 22040
You're reusing View's for different elements (a good thing) but your ViewHolder object's reference will still hold references to the previous elements objects. It would be better (and work) to re-find them.
Upvotes: 1