2016-11-18 89 views
1

我有一個listView工作/顯示一些數據作爲標題和副標題,所有工作正常,但每當我向上滾動列表或convertView回收時,最後一個元素的副標題得到重複而標題是不是,什麼問題..ListView元素的子元素在某個位置後重復

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

下的「其他」部分的「viewHolder.m_profileDetails」設置自己的文本位置編號0,而它的位置是8或9列表。

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

回答

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

讓我知道如果得到任何錯誤它

+0

感謝阿尼爾...它的工作。 – sam

+0

太棒了,你的代碼是正確的,但你做錯了代碼安排這就是爲什麼你的看法只能重複使用前四項創建 –

+0

現在檢查getView()方法我已經把註釋,這有助於你理解實際工作的列表適配器和使用viewHolder類。 –