ListView元素的子元素在某个位置后重复

时间:2016-11-18 10:14:09

标签: android listview convertview

我有一个listView正在工作/显示一些数据作为标题和副标题,一切正常但每当我向上滚动列表或convertView回收时,最后一个元素的副标题重复,而标题不是,是什么问题..

 @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;
}
<\ n>在“else”部分下,“viewHolder.m_profileDetails”将其文本设置为位置编号0,而位置在列表中的位置为8或9。

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

1 个答案:

答案 0 :(得分:1)

试试这个getView方法: -

 @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)
    {
        //always define viewholder object here and also link your xml components like textview using findViewById() method.

        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
    {
        // This tag viewHolder object helps you to reuse your object.
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.m_radioButton.setChecked(false);
    }
    // Always do setText(),onClick like operation on components here so here viewHolder decide which UI components need to use.

    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;
}

如果在其中收到任何错误,请告诉我