2012-12-26 65 views
1

我正在使用ListView和自定義適配器來獲取JSONArray(來自Facebook請求)。每個ListView項目都有兩個TextViews和一個或兩個ImageViews,具體取決於帖子類型。每個列表項還有一個LinearLayout,我稱其爲「評論框」。ListView中的動態LinearLayout項目

當我在xml中定義列表項時,評論框僅包含標題(「評論和贊」)的TextView。在自定義適配器中,解析註釋數據(正確地希望),併爲每個註釋以編程方式添加TextViews,這樣我就不會創建空白的TextView並將其充氣爲空。

我遇到的問題,這似乎是一個常見的ListView回收問題,是評論框不滾動列表時保留其內容。我查看了JSON數據,數組中的每個對象都具有相同的「註釋」字段,所以我不會認爲數據中的任何內容都是空的,特別是如果所有其他數據加載並填充完美。我不知道我做錯了什麼,因爲所有其他的工作,但這。這是我的適配器的getView()方法。

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

    final ViewHolder holder; 

    try { 
     jsonObject = getItem(position); 
     jsonFrom = jsonObject.getJSONObject("from"); 
     postType = jsonObject.getString("type"); 
     posterName = jsonFrom.getString("name"); 
     posterID = jsonFrom.getString("id"); 
     posterPhoto = "http://graph.facebook.com/" + posterID + "/picture?type=square"; 
     if (jsonObject.has("name")) { 
      posterMessage = jsonObject.getString("name"); 
     } 
     else if (jsonObject.has("story")){ 
      posterMessage = jsonObject.getString("story"); 
     } 
     else if (jsonObject.has("message")){ 
      posterMessage = jsonObject.getString("message"); 
     } 
     else{ 
      posterMessage = "No message."; 
     } 
     if(jsonObject.has("picture")){ 
      posterImageURL = jsonObject.getString("picture"); 
     } 
     commentsData = jsonObject.getJSONObject("comments"); 
     commentsCount = commentsData.getInt("count"); 


    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.post_holder,null); 
     holder = new ViewHolder(); 
     holder.posterName = (TextView) convertView.findViewById(R.id.posterName); 
     holder.posterMessage = (TextView) convertView.findViewById(R.id.posterMessage); 
     holder.posterProfilePhoto = (ImageView) convertView.findViewById(R.id.posterProfilePic); 
     holder.posterImage = (ImageView) convertView.findViewById(R.id.posterImage); 
     holder.commentsBox = (LinearLayout) convertView.findViewById(R.id.commentsBox); 
     if(postType.equals("photo")){ 
      holder.posterImage.setVisibility(View.VISIBLE); 
     } 
     if(commentsCount!=0 && commentsCount!=null){ 
      try { 
       commentsArray = commentsData.getJSONArray("data"); 
       for(int index = 0; index<commentsArray.length(); index++){ 
        comment = commentsArray.getJSONObject(index); 
        commenterName = comment.getJSONObject("from").getString("name"); 
        commenterId = comment.getJSONObject("from").getString("id"); 
        commentMessage = comment.getString("message"); 
        TextView commentMessageHolder = new TextView(activity); 
        commentMessageHolder.setText(Html.fromHtml("<b>"+commenterName+"</b>"+" "+commentMessage)); 
        holder.commentsBox.addView(commentMessageHolder, index+1); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 


     } 

     convertView.setTag(holder); 
    } 
    else { 
     holder = (ViewHolder) convertView.getTag(); 
     if(postType.equals("photo")){ 
      holder.posterImage.setVisibility(View.VISIBLE); 
     } 
     else{ 
      holder.posterImage.setVisibility(View.GONE); 
     } 
    } 

    if (jsonObject != null) { 
     holder.posterName.setText(posterName); 
     if (posterMessage != null) { 
      holder.posterMessage.setText(posterMessage); 
     } else { 
      holder.posterMessage.setText("No message for this post."); 
     } 

     imageLoader.displayImage(posterPhoto,holder.posterProfilePhoto); 

     if(postType.equals("photo")){ 
      if(posterImageURL != null){ 
       holder.posterImage.setTag(posterImageURL); 
       imageLoader.displayImage(posterImageURL, holder.posterImage); 
      } 
     } 
    } 

    return convertView; 

} 

回答

1

我有問題,這似乎是一個常見的ListView回收 問題是,當滾動通過列表 評論框不保留其內容。

的主要問題是,你建立的意見TextViews何時convertViewnull的情況下,當用戶將滾動ListView你會與最初設定的行視圖(帶有註釋)最終是你不會修改不需要的位置。將註釋for循環移到if子句的外部,在此檢查convertViewnull

此外,你可能想緩存該json解析,更不用說你需要考慮不要添加註釋TextViews作爲回收行視圖可能已經有自己的評論TextViews

+0

就是這樣。非常感謝!我在Android編程方面很新穎,那麼將使用什麼過程來緩存json解析?謝謝。 – Wenger

+0

你可以有一個自定義對象的列表(帶有數據的大小),它將保存來自json結構的所有數據值對應於一行。在'getView'方法中,您將檢查此列表以查看是否爲該行存儲了某些內容。如果你不(當你第一次到達那個位置時),那麼進行當前提取並將數據放入列表中。下一次你到達那一行時,你已經解析了json結構,並且只需從列表中檢索數據。 – Luksprog