【问题标题】:Listview image repeat from server in rowListview 图像从服务器连续重复
【发布时间】:2014-02-28 13:36:58
【问题描述】:

当滚动图像(延迟加载)被上传以重复前两个时,在我的 android 列表视图中。 照片从在线服务器上传 此外,在 android 4.4 上,图片非常非常大。 为什么?解决方案?

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        Log.d(TAG, "POS: " + position);         

        if (convertView == null) {

            convertView = mLayoutInflater.inflate(R.layout.row_posts, parent, false);

            holder = new ViewHolder();
            holder.tvTitolo = (TextView) convertView.findViewById(R.id.tvTitoloPost);
            holder.tvData = (TextView) convertView.findViewById(R.id.tvData);
            holder.ivPost = (ImageView) convertView.findViewById(R.id.ivPost);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
            holder.tvExcerpt = (TextView) convertView.findViewById(R.id.tvExcerpt);
            holder.wvPost = (WebView) convertView.findViewById(R.id.wvPost);
            holder.tvIDPost = (TextView) convertView.findViewById(R.id.tvIDPost);
            holder.tvUrlPost = (TextView)convertView.findViewById(R.id.tvURLPost);
            holder.tvCommPost = (TextView)convertView.findViewById(R.id.tvCommPost);
             convertView.setTag(holder);             
        }else {
            holder = (ViewHolder) convertView.getTag();
        }final Post post = getItem(position);            
        holder.tvTitolo.setText(post.getTitle());
        holder.tvData.setText(post.getDate());      
        WebSettings webSettings = holder.wvPost.getSettings();
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setLightTouchEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        holder.wvPost.setFocusable(false);
        holder.wvPost.setClickable(false);
        holder.wvPost.setFocusableInTouchMode(false);
        holder.wvPost.setLongClickable(false);
holder.wvPost.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        holder.wvPost.loadUrl(post.getThumbnail());
        Log.v(TAG, "html: <!DOCTYPE html><html><body style = \"text-align:center\"><img src= " + post.getThumbnail() + " alt=\"\"></body></html>");
        holder.wvPost.loadDataWithBaseURL(null,"<!DOCTYPE html><html><body style = \"text-align:center\"><img src= " + post.getThumbnail() + " alt=\"\"></body></html>","text/html", "UTF-8", null);
        holder.wvPost.setClickable(false);

    if (post.getThumbnail().equals("null")) {
            imageLoader.displayImage(null, holder.ivPost);
        }else {
            imageLoader.displayImage(post.getThumbnail(), holder.ivPost);
        }        
        Log.d(TAG, "THUMB: " + post.getThumbnail());            
        /* if(post.getThumbnail().equals("null")){
            holder.ivPost.setVisibility(View.GONE);
        }else{
            imageLoader.displayImage(post.getThumbnail(), holder.ivPost);
            holder.ivPost.setVisibility(View.VISIBLE);          
        }*/

        imageLoader.displayImage(post.getThumbnail(), holder.ivPost);

        holder.tvExcerpt.setText(post.getExcerpt());
        holder.tvContent.setText(post.getContent());
        holder.tvIDPost.setText(post.getId());
        holder.tvUrlPost.setText(post.getThumbnail());

        String comments = "";

        for (int i = 0; i < post.getmComments().size(); i++) {
            comments = "<b>" + post.getmComments().get(i).getName() + 
                        "</b><br>" + 
                        post.getmComments().get(i).getContent() + 
                        "<br><b>on </b>" + 
                        post.getmComments().get(i).getDate() + 
                        "<br><br>";
        }           
        holder.tvCommPost.setText(comments);                        
        return convertView;
    }}

XML row_post:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="#ffffff" >

    <TextView
        android:id="@+id/tvTitoloPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="Titolo"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvTitoloPost"
        android:layout_below="@+id/tvTitoloPost"
        android:text="Data" />

     <WebView
         android:id="@+id/wvPost"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@+id/tvData"
         android:layout_centerHorizontal="true"
         android:layout_marginLeft="10dp"
         android:layout_marginTop="10dp"
         android:background="#ffffff"

          />

    <ImageView
        android:id="@+id/ivPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvData"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/default_avatar" 
        android:visibility="gone" />

    <TextView
        android:id="@+id/tvExcerpt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/wvPost"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="15dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wvPost"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="15dp"
        android:maxLines="4"
        android:text="TextView"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tvIDPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvContent"
        android:layout_below="@+id/tvContent"
        android:text="TextView"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tvURLPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvIDPost"
        android:layout_below="@+id/tvContent"
        android:text="TextView"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tvCommPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvURLPost"
        android:layout_below="@+id/tvContent"
        android:text="TextView"
        android:visibility="gone" />

</RelativeLayout>

【问题讨论】:

  • 分享 R.layout.row_posts 。

标签: android listview android-listview android-webview universal-image-loader


【解决方案1】:

也许您已经缓存了图像。使用应用程序管理器删除数据并重新启动。

已编辑:确保在 getView 中只调用一次 displayImage 方法:

避免将 null 与等号进行比较:

if (post.getThumbnail().equals("null"))

改为如下使用:

if (!TextUtils.isEmpty(post.getThumbnail()))     

最后:

//!TextUtils.isEmpty performs 2 checks: 1) Object!=null 2) Is not an empty String
if (!TextUtils.isEmpty(post.getThumbnail())) {
    imageLoader.displayImage(post.getThumbnail(), holder.ivPost);
}
// No more callings to displayImage!!

【讨论】:

  • 好的,然后删除多余的代码:imageLoader.displayImage(post.getThumbnail(), holder.ivPost);对于 getview 的每个实例,您总是调用两次 .. 之后检查日志: Log.d(TAG, "THUMB: " + post.getThumbnail());检查每个位置的 url 是否重复。另外,请发布您的 getItem(position) 代码
  • 我在else里面注释了代码,情况有所改善。但是,在几纳秒内拍摄更多照片,而不是很快上传。我不明白我应该发布什么?
  • 贴出这些东西: 1) getItem(int position) 方法的代码。 2)Log.d(TAG, "THUMB: " + post.getThumbnail()); 生成的日志报告
  • POS: 0 html: html> THUMB: url img POS: 1 html: THUMB: url img POS: 2 V/app(11581): html: D/app(11581): THUMB: url img I/chromium(11581): [INFO:async_pixel_transfer_manager_android.cc(56)] 不支持异步像素传输 D/app(11581): POS: 0 POS: 1个等等……
  • 我已经编辑了你的代码:pastebin.com/w156Fkna 基本上,当你调用 displayImage(调用一次)时我已经改变了: if (!TextUtils.isEmpty(post.getThumbnail())) { imageLoader.displayImage (post.getThumbnail(), holder.ivPost); }
猜你喜欢
  • 2014-12-25
  • 2010-11-20
  • 2015-11-25
  • 1970-01-01
  • 2011-07-16
  • 2018-05-30
  • 1970-01-01
  • 2023-03-10
  • 2013-06-22
相关资源
最近更新 更多