I have a list view where the two names are displayed based on some conditions. Sometimes the first name alone is displayed and sometimes the first name and the second name is displayed in the view(each row) of the list view. All is fine when the list appears initially. But when I scroll through the list, the issue happens.
Row 0 goes messy in the list view. It displays the second name though I have programmed it to be invisible.The same happens to other rows when I scroll further down and come back.
I got to know this is due to list recycling. But I have implemented the code mentioned to avert this situation. But this still keep happening. Can you help me in overcoming this please.
The code snippet is below. Thanks for your time.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.firstcontactname = (TextView) view.findViewById(R.id.firstcontactname);
holder.secondcontactname = (TextView) view.findViewById(R.id.secondcontactname);
}
if (holder.firstcontactname != null && null != objBean.getfirstcontactname()
&& objBean.getfirstcontactname().trim().length() > 0) {
//holder.firstcontactname.setText(Html.fromHtml("" + "<b>"+"<font color='black'>"+objBean.getfirstcontactname()));
holder.firstcontactname.setText(Html.fromHtml("" + "<b>"+objBean.getfirstcontactname()));
}
if (holder.secondcontactname != null && null != objBean.getsecondcontactname()
&& objBean.getsecondcontactname().trim().length() > 0) {
//holder.secondcontactname.setText(Html.fromHtml("" + "<b>"+"<font color='black'>"+objBean.getsecondcontactname()));
holder.secondcontactname.setText(Html.fromHtml("" + "<b>"+objBean.getsecondcontactname()));
}
if (objBean.getsecondcontactname()==null ) {
holder.secondcontactname.setVisibility(View.INVISIBLE);
}
return view;
}