ListView 元素的子元素在某个位置后重复
ListView element's sub element is repeating after a certain position
我有一个 listView,它是 working/displaying 一些数据作为标题和副标题,一切正常,但每当我向上滚动列表或 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;
}
在 "else" 部分下 "viewHolder.m_profileDetails" 将他们的文本设置为位置编号 0,而它在列表中的位置是 8 或 9。
private static class ViewHolder{
RadioButton m_radioButton;
TextView m_profileName;
TextView m_profileDetails;
LinearLayout m_profileLyt;
}
试试这个 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;
}
如果有任何错误请告诉我
我有一个 listView,它是 working/displaying 一些数据作为标题和副标题,一切正常,但每当我向上滚动列表或 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;
}
在 "else" 部分下 "viewHolder.m_profileDetails" 将他们的文本设置为位置编号 0,而它在列表中的位置是 8 或 9。
private static class ViewHolder{
RadioButton m_radioButton;
TextView m_profileName;
TextView m_profileDetails;
LinearLayout m_profileLyt;
}
试试这个 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;
}
如果有任何错误请告诉我