diffuculty in appending images dynamically in an Custom List View
- by ganesh
Hi
I have written an custom List view which binds images according to the result from a feed
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.row_for_stopnames, null);
holder = new ViewHolder();
holder.name = (TextView)convertView.findViewById(R.id.stop_name);
holder.dis = (TextView)convertView.findViewById(R.id.distance);
holder.route_one=(ImageView)convertView.findViewById(R.id.one);
holder.route_two=(ImageView)convertView.findViewById(R.id.two);
holder.route_three=(ImageView)convertView.findViewById(R.id.three);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(elements.get(position).get("stop_name"));
holder.dis.setText(elements.get(position).get("distance"));
String[] route_txt=elements.get(position).get("route_name").split(",");
for(int i=0;i<route_txt.length;i++)
{
if(i==0)
{
holder.route_one.setBackgroundResource(Utils.getRouteImage().get(stop_txt[0]));
}
else if(i==1)
{
holder.route_two.setBackgroundResource(Utils.getRouteImage().get(stop_txt[1]));
}
else if(i==2)
{
holder.route_three.setBackgroundResource(Utils.getRouteImage().get(stop_txt[2]));
}
}
convertView.setOnClickListener(new OnItemClickListener(position,elements));
return convertView;
}
class ViewHolder {
TextView name;
TextView dis;
ImageView route_one;
ImageView route_two;
ImageView route_three;
}
for every stop name there may be route_names, maximum of three routes.I have to bind images according to the number of route names.This is what I tried to do by the above code .This works fine until I start scrolling up and down .When I do so the route images gets displayed where it does not want to be,this behaviour is unpredictable.I will be glad if someone explain me why this happens,and the best way to do this.The getRouteImage method of Utils class returns HashMap with key String and value a drawable