ListView项中的动态LinearLayout

时间:2012-12-26 16:14:42

标签: android android-listview

我正在使用带有自定义适配器的ListView,该适配器获取JSONArray(来自Facebook请求)。每个ListView项目都有两个TextView和一个或两个ImageViews,具体取决于帖子类型。每个列表项还有一个我称之为“注释框”的LinearLayout。

当我在xml中定义列表项时,注释框仅包含标题的TextView(“评论和喜欢”)。在自定义适配器中,解析注释数据(正确的我希望),并以编程方式为每个注释添加TextViews,这样我就不会创建任何空文本框并为其充气。

我遇到的问题,似乎是一个常见的ListView回收问题,是在滚动列表时注释框不保留其内容。我查看了JSON数据,并且数组中的每个对象都有相同的“comments”字段,所以我不认为我对数据中的任何内容都是null,特别是如果所有其他数据加载并填充完全正常。我不知道我做错了什么,因为其他一切都有效但是这个。这是我的适配器的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 个答案:

答案 0 :(得分:1)

  

我遇到的问题,这似乎是一个常见的ListView回收   问题是,评论框何时不保留其内容   滚动列表。

主要问题是,当TextViewsconvertView时用户将滚动{{1}时,您只为null 构建评论你最终会得到那些你不会为不想要的位置修改的最初设置的行视图(带注释)。将评论ListView循环移到if子句之外,在for convertViewnull

此外,您可能希望缓存该json解析,更不用说您需要考虑不要过度添加注释TextViews,因为已回收的行视图可能已经拥有其自己的注释{{1} }。

相关问题