2011-11-10 2 views
1

に動作しません:GridViewのは、私は次のようprolemを持って正しく

私は、インターネットから画像をダウンロードし、GridViewコントロールにそれらを提供ImageAdapterによって供給されたGridViewを持っています。これは素晴らしいことですが、私を驚かせる1つの問題があります。問題は、画面を埋めるはずの最初の10枚の画像が正しく読み込まれていることです。しかし、11番目は空白で表示され、何らかの未知の理由でconvertViewから直接取得されたようです。イメージ12は正しく表示され、下のすべてのイメージは有効なURLがダウンロードされていても空白です。

これは、それが唯一のアダプタに渡された文字列(リンク)の配列を持っているのでGridActivity.javaが提供されていない

package com.wm.grid; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 
    public String[] mThumbIds; 
    public Bitmap[] bmp; 
    public Bitmap bitmapPlaceholder; 
    public ImageAdapter(Context c,String[] m) { 
     mContext = c; 
     mThumbIds = m; 
     initBitmapListWithPlaceholders(); 
    } 
    public void initBitmapListWithPlaceholders(){ 
     int count = mThumbIds.length; 
     bmp = new Bitmap[count]; 
     for(int i=0;i<count;i++){ 
      bmp[i]=bitmapPlaceholder; 
     } 
    } 
    public int getCount() {  
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.v("POSITION","P"+position+convertView); 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(210, 150)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(1, 1, 1, 1); 
      Bitmap bm = getBitmap(setURL(mThumbIds[position]),position); 
      bmp[position] = bm; 
      imageView.setImageBitmap(bm); 


     } else { 
      imageView = (ImageView) convertView; 
      imageView.setImageBitmap(bmp[position]); 
     } 
     //imageView.setImageBitmap(getBitmap(setURL(mThumbIds[position]))); 
     //imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 
    private URL setURL (String urls){ 
     try{ 
      URL urli = new URL(urls); 
      return urli; 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
    return null; 
    } 
    private Bitmap getBitmap (URL url,int position){ 

     try{ 
      Log.v("POSITION","Loading "+position+url); 
      Bitmap bm = BitmapFactory.decodeStream((InputStream) url.getContent()); 
      return bm; 
     } 
    catch (IOException e) { 
     e.printStackTrace(); 
     } 
    return null; 
    } 
/* public Integer[] AddItems(Integer[] a){ 
     final Integer[] aThumbIds; 
     final int i; 
     aThumbIds = a; 
     Integer[] a2 = new Integer[mThumbIds.length + aThumbIds.length]; 
     System.arraycopy(mThumbIds, 0, a2, 0, mThumbIds.length); 
     System.arraycopy(aThumbIds, 0, a2, mThumbIds.length, aThumbIds.length); 
     mThumbIds = a2; 
     return mThumbIds; 
    } 
    */ 
} 

私ImageAdapterです。

main.xmlはgridviewなので、私はそれが何かとは考えていません。

さらに、いくつかのデバッグポイントの出力があり、非常に奇妙です。

11-10 14:55:28.700: V/POSITION(12936): Loading 0 //Loading the first image 
11-10 14:55:28.785: V/POSITION(12936): [email protected]//First image found in ConvertView (why ? it is already loaded ... :() 
11-10 14:55:28.790: V/POSITION(12936): [email protected]//Same thing 
11-10 14:55:28.790: V/POSITION(12936): P1null//All OK convertView is null load image from web 
11-10 14:55:28.790: V/POSITION(12936): Loading 1 
11-10 14:55:28.895: V/POSITION(12936): P2null 
11-10 14:55:28.895: V/POSITION(12936): Loading 2 
11-10 14:55:29.070: V/POSITION(12936): P3null 
11-10 14:55:29.070: V/POSITION(12936): Loading 3 
11-10 14:55:29.175: V/POSITION(12936): P4null 
11-10 14:55:29.175: V/POSITION(12936): Loading 4 
11-10 14:55:29.290: V/POSITION(12936): P5null 
11-10 14:55:29.290: V/POSITION(12936): Loading 5 
11-10 14:55:29.420: V/POSITION(12936): P6null 
11-10 14:55:29.420: V/POSITION(12936): Loading 6 
11-10 14:55:29.500: V/POSITION(12936): P7null 
11-10 14:55:29.505: V/POSITION(12936): Loading 7 
11-10 14:55:29.580: V/POSITION(12936): P8null 
11-10 14:55:29.580: V/POSITION(12936): Loading 8 
11-10 14:55:29.675: V/POSITION(12936): P9null 
11-10 14:55:29.675: V/POSITION(12936): Loading 9 
11-10 14:55:29.800: V/POSITION(12936): P0null//again !? P0 was loaded in first place why does it try to load it again ?!... and it is even NULL ?! 
11-10 14:55:29.800: V/POSITION(12936): Loading 0 
11-10 14:55:29.910: V/POSITION(12936): [email protected] 
===================== 
11-10 15:02:00.435: V/POSITION(12936): [email protected]//Now image 10 was NEVER loaded before why the heck it is in the convertView !? 
11-10 15:02:00.435: V/POSITION(12936): P11null//Image 11 is loaded correctly. 
11-10 15:02:00.435: V/POSITION(12936): Loading 11 
=====================(Scrolling down is even more questionable) 
11-10 15:03:10.740: V/POSITION(12936): [email protected]//Where the heck convertView found this image (never loaded) 
11-10 15:03:10.740: V/POSITION(12936): [email protected]//Same applies for this one. 

上記の結果は、画像10 12と13と下にあるすべての画像が任意の手がかりが歓迎さ

空白であることです。事前にどうもありがとうございました。

EDIT:もう1つです。私はこのようなコードを変更する場合:

if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(210, 150)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(1, 1, 1, 1); 
      Bitmap bm = getBitmap(setURL(mThumbIds[position]),position); 
      bmp[position] = bm; 
      imageView.setImageBitmap(bm); 


     } else { 
      imageView = (ImageView) convertView; 
      imageView.setImageBitmap(getBitmap(setURL(mThumbIds[position]),position));//DOWNLOAD IMAGE AGAIN 
     } 

をアプリケーションは、2つの遅い取り組んでいるが、それは常に画像をダウンロードしているので、画像が正しく表示されます。

答えて

0

ここであなたのプロジェクトにキャッシュを実装しようとすると、link

関連する問題