Change getView
You use a ViewHolder
pattern. You initialize views when convertView
is null. You set the tag to convertView
.
convertView.setTag(holder);
If convertView
is not null
holder = (ViewHolder) convertView.getTag();
As Simon suggested in his commented
// convertView.setTag(holder); // commented
holder = (ViewHolder) convertView.getTag(); // using get tag
// this line probably gives you NPE
How ListView's recycling mechanism works
Probably the above link has a better explanation how list recycling works
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Using ViewHolder
Pattern helps in smooth scrolling and performance.
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_available_layout, parent,false);
holder = new ViewHolder();
holder.txt_plname = (TextView) convertView.findViewById(R.id.textView_planname);
holder.txt_price = (TextView) convertView.findViewById(R.id.textView_price);
holder.txt_size = (TextView) convertView.findViewById(R.id.textView_size);
holder.btn_buy_ = (Button) convertView.findViewById(R.id.button_buy);
convertView.setTag(holder);
// this was commented. no need to do that
}
else
{
holder = (ViewHolder) convertView.getTag();
// if convertVIew is not null initialize the holder using convertView.getTag
}
Available_contents contents = originalist.get(position);
holder.txt_price.setText("K "+contents.price);
// set text to text view and update it here
holder.txt_size.setText(contents.size);
holder.txt_plname.setText(contents.planname);
}
return convertView;
}