I am using a ListView with sections and section headers. Below is the GetView method of adapter without ViewHolder class, which works fine but when scrolled several times, freezes the UI and also kills the app on several devices.
@Override
public View getView(int position, View v, ViewGroup parent)
{
//View v = convertView;// = convertView;
//System.out.println("getView " + position + " " + convertView);
final Item i = items.get(position);
if (i != null)
{
if(i.isSection())
{
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setTypeface(StaticUtils.sTypeFace(context));
sectionView.setText(si.getTitle());
v.setEnabled(false);
}
else
{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final ImageView mImg = (ImageView)v.findViewById(R.id.list_item_entry_drawable);
mImg.getLayoutParams().height = mIvPrams;
mImg.getLayoutParams().width = mIvPrams;
title.setTypeface(StaticUtils.sTypeFace(context));
title.setSelected(true);
if (title != null)
title.setText(ei.title);
imageLoader.displayImage(ei.imgUrl, mImg, options, animateFirstListener);
}
}
return v;
}
Now, I tried to implement ViewHolder class to it to improve the freeze issue. Below is the code with ViewHolder class that I implemented for the adapter. However, when I scroll with below implementation, the ListView gets all jumbled up. It is not able to hold the index of its elements. If I try to do changes, I also get NullPointerException at times when I scroll back from bottom to top.
@Override
public View getView(final int position, View v, ViewGroup parent)
{
//View v = null;// = convertView;
//System.out.println("getView " + position + " " + convertView);
final Item i = items.get(position);
if (i != null)
{
if(i.isSection())
{
/*if (convertView == null) {
v = (View) vi.inflate(R.layout.list_item_section, null);
// Do some initialization
} else {
v = convertView;
}*/
if(v==null)
{
mHolder = new ViewHolder();
v = vi.inflate(R.layout.list_item_section, null);
mHolder.s = (SectionItem)i;
mHolder.mSectionView = (TextView) v.findViewById(R.id.list_item_section_text);
v.setTag(mHolder);
}
else
{
mHolder=(ViewHolder)v.getTag();
//v = convertView;
}
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
//final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
mHolder.mSectionView.setTypeface(StaticUtils.sTypeFace(context));
mHolder.mSectionView.setText(mHolder.s.getTitle());
v.setEnabled(false);
}
else
{
//v = vi.inflate(R.layout.list_item_entry, null);
if (v == null) {
mHolder = new ViewHolder();
v = (View) vi.inflate(R.layout.list_item_entry, null);
mHolder.e = (EntryItem)i;
mHolder.mTitle = (TextView)v.findViewById(R.id.list_item_entry_title);
mHolder.mImg = (ImageView)v.findViewById(R.id.list_item_entry_drawable);
mHolder.mImg.getLayoutParams().height = mIvPrams;
mHolder.mImg.getLayoutParams().width = mIvPrams;
v.setTag(mHolder);
// Do some initialization
} else {
mHolder=(ViewHolder)v.getTag();
}
//mHolder.mTitle.setTypeface(StaticUtils.sTypeFace(context));
//mHolder.mTitle.setSelected(true);
if (mHolder.mTitle != null)
mHolder.mTitle.setText(mHolder.e.title);
imageLoader.displayImage(mHolder.e.imgUrl, mHolder.mImg, options, animateFirstListener);
}
}
return v;
}
public class ViewHolder
{
TextView mSectionView, mTitle;
ImageView mImg;
EntryItem e;
SectionItem s;
}
I hope to get a solution on how can I improve my code and write a proper ViewHolder class for this adapter.