sam sam - 19 days ago 5
Android Question

ListView element's sub element is repeating after a certain position

I have a listView which is working/displaying some data as Title and subtitle ,all is working fine but whenever I scroll up the list or the convertView recycles, the subtitle of last element get repeated while the Title is not, whats the issue..

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{


final ViewHolder viewHolder;
m_oActivatedProfilePreferences =m_oContext.getSharedPreferences(m_kSHARED_PREF_PROFILE_KEY, Context.MODE_PRIVATE);
final CProfileDataSource profileDataSource = new CProfileDataSource(m_oContext);
final List<CUserProfile> profileName = profileDataSource.getAllProfiles();

if(convertView==null)
{
viewHolder=new ViewHolder();
convertView=layoutInflater.inflate(R.layout.profile_description,parent,false);
viewHolder.m_profileName=(TextView) convertView.findViewById(R.id.profilename);
viewHolder.m_radioButton=(RadioButton)convertView.findViewById(R.id.radioButton);
viewHolder.m_profileDetails=(TextView)convertView.findViewById(R.id.pro_details);
viewHolder.m_profileLyt=(LinearLayout)convertView.findViewById(R.id.profile);
if(position==0)
{
viewHolder.m_profileDetails.setText(R.string.general_profile_description);
}else if(position==1)
{
viewHolder.m_profileDetails.setText(R.string.sleep_profile_description);
}else if(position==2)
{
viewHolder.m_profileDetails.setText(R.string.ssaver_profile_description);
}else
{
//here for the 8th position of list it displays the text as position 0

viewHolder.m_profileDetails.setText(R.string.profile_desc);
}
convertView.setTag(viewHolder);
viewHolder.m_radioButton.setChecked(false);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.m_radioButton.setChecked(false);
}
return convertView;
}


under the "else" part the "viewHolder.m_profileDetails" set their text as position number 0, while it's position is 8 or 9 in the list.

private static class ViewHolder{
RadioButton m_radioButton;
TextView m_profileName;
TextView m_profileDetails;
LinearLayout m_profileLyt;
}

Answer

try this getView method:-

 @Override
public View getView(final int position, View convertView, ViewGroup parent)
{


    final ViewHolder viewHolder;
    m_oActivatedProfilePreferences =m_oContext.getSharedPreferences(m_kSHARED_PREF_PROFILE_KEY, Context.MODE_PRIVATE);
    final CProfileDataSource profileDataSource = new CProfileDataSource(m_oContext);
    final List<CUserProfile> profileName = profileDataSource.getAllProfiles();

    if(convertView==null)
    {
        viewHolder=new ViewHolder();
        convertView=layoutInflater.inflate(R.layout.profile_description,parent,false);
        viewHolder.m_profileName=(TextView) convertView.findViewById(R.id.profilename);
        viewHolder.m_radioButton=(RadioButton)convertView.findViewById(R.id.radioButton);
        viewHolder.m_profileDetails=(TextView)convertView.findViewById(R.id.pro_details);
        viewHolder.m_profileLyt=(LinearLayout)convertView.findViewById(R.id.profile);

        convertView.setTag(viewHolder);
        viewHolder.m_radioButton.setChecked(false);
    }
    else
    {
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.m_radioButton.setChecked(false);
    }
    if(position==0)
        {
            viewHolder.m_profileDetails.setText(R.string.general_profile_description);
        }else if(position==1)
        {
            viewHolder.m_profileDetails.setText(R.string.sleep_profile_description);
        }else if(position==2)
        {
            viewHolder.m_profileDetails.setText(R.string.ssaver_profile_description);
        }else
        {
           //here for the 8th position of list it displays the text as position 0

            viewHolder.m_profileDetails.setText(R.string.profile_desc);
        }
  return convertView;
}

Let me know if getting any error in it