Reputation: 1957
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;
}
Upvotes: 0
Views: 836
Reputation: 6406
this is not your questions answere.but instead of using this line:
if ((items == null) || ((position + 1) > items.size()))
return view;
override this method
@Override
public int getCount() {
return items.size();
}
now if there is something in items list then only getview will get called.
now for your question's answer i would say if you want a simple solution : inflate a new view every item, don not check for (view==null) and you can remove the holder pattern as well. keep it simple. i know it is not efficient way but if your list Item view is simple and in scrolling its fine with you then only use it. else you need to do a little more work to make it smooth. let me know if this not work for you.
Upvotes: 1
Reputation: 1015
First off, you should find the TextViews
holder.firstcontactname = (TextView) view.findViewById(R.id.firstcontactname);
inside the if(view == null) block right after you instantiate your new Holder().
Second, because of view recycling you need to take care of every 'else' case. Right now you are checking if holder.firstcontactname is not null then setting the text, if it IS null (the 'else' case) you need to set the text to empty or hide the text field otherwise the text will still be there from the old view that was inflated in a previous row.
Upvotes: 1