2011-11-22 86 views
0

填充列表項在我的應用程序,以填充一個ListView我使用自定義適配器,因爲一個listitem由3 TextView秒和1 ImageView。 每次圖像都從url中獲取。Android的問題,在一個ListView

因此,當我啓動此活動時,需要花費很多時間,因爲它下載所有圖像,然後填充列表。

所以我想沒有圖像列表應填充第一隻有Textview s和此後,只有圖像應該來。

我該怎麼做?

+1

你需要使用一個單獨的線程;這裏有一個很好的例子。 http://stackoverflow.com/questions/7729133/using-asynctask-to-load-images-in-listview – C0deAttack

回答

2

所謂具有AsyncTask

例如直接從文件加載您的圖像:

public void onClick(View v) { 
    new DownloadImageTask().execute("http://example.com/image.png"); 
} 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    /** The system calls this to perform work in a worker thread and 
     * delivers it the parameters given to AsyncTask.execute() */ 
    protected Bitmap doInBackground(String... urls) { 
     return loadImageFromNetwork(urls[0]); 
    } 

    /** The system calls this to perform work in the UI thread and delivers 
     * the result from doInBackground() */ 
    protected void onPostExecute(Bitmap result) { 
     mImageView.setImageBitmap(result); 
    } 
} 
+0

你能舉個例子嗎? – Jyosna

+0

是的,打開文檔。 –

+0

這個東西我必須寫在我的自定義adaoter寫? – Jyosna

0

基本的想法是在您的應用程序中已經有一個加載圖像。 然後使用asyncTask或線程加載圖像。

某些代碼到開始:

適配器

public class ImageAdapter extends BaseAdapter { 
private static final String TAG = "Image Adapter"; 
int mGalleryItemBackground; 
private Context mContext; 
private GridView mView; 


/** URL-Strings to some remote images. */ 
private String[] mRemoteImagesURL ; 
private Bitmap[] loadedImages; 


public ImageAdapter(Context c,String[] remoteImagesURL,GridView v) { 
    mContext = c; 
    TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); 
    mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0); 
    attr.recycle(); 
    mView = v; 
    mRemoteImagesURL=remoteImagesURL; 
    loadedImages = new Bitmap[mRemoteImagesURL.length]; 

} 


public int getCount() { 
    return mRemoteImagesURL.length; 
} 

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

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

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

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.gallery_item, null); 
    } 


    ImageView imageView = (ImageView) convertView.findViewById(R.id.FrontImageView); 

    /* when image is already down-loaded then load that image */ 
    if(loadedImages[position]!=null) 
     imageView.setImageBitmap(loadedImages[position]); 
    else 
     imageView.setImageResource(R.drawable.loading); 

    imageView.setBackgroundResource(mGalleryItemBackground); 


    return convertView; 
} 

public void loadImage(int position){ 
    Bitmap bm; 

     try { 
      /* Open a new URL and get the InputStream to load data from it. */ 
      URL aURL = new URL(mRemoteImagesURL[position]); 
      URLConnection conn = aURL.openConnection(); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      /* Buffered is always good for a performance plus. */ 
      BufferedInputStream bis = new BufferedInputStream(is); 
      /* Decode url-data to a bitmap. */ 
      bm = BitmapFactory.decodeStream(bis); 
      bis.close(); 
      is.close(); 
      loadedImages[position] =bm; 

    } catch (Exception e) { 

      Log.e(TAG, "Remote Image Load Exception"+e); 
    } 

}

public void setLoadedImage(int position) 
{ 
    Log.d(TAG, "Position "+position); 
    View childView= mView.getChildAt(position); 
    if(loadedImages[position]!=null && childView != null) 
    { 
     ImageView imageView= (ImageView) childView.findViewById(R.id.FrontImageView); 
     imageView.setImageBitmap(loadedImages[position]);   
    } 
} 

} 私人無效updateImagesAsThread(){ 線程t =新主題() {

  public void run() 
      { 
       try { 


       for(int i=0;i<imageAdapter.getCount();i++) 
       { 
       imageAdapter.loadImage(i); 
       listAdapterHandler.sendEmptyMessage(i); 
       } 
       } 
       catch (Exception e) { 
        // TODO: handle exception 
        Log.e(TAG,"UpdateImageAsThread "+e); 
       } 


      } 
     }; 
     t.start(); 

    } 

private Handler listAdapterHandler = new Handler() 
{ 


    @Override 
    public void handleMessage(Message msg) 
    { 

     switch (msg.what) 
     { 
      case -1: 
       Log.d(TAG, "here in the handle..."); 
       break; 
      default: 
       Log.d(TAG, "here in the handle default..."); 
       imageAdapter.setLoadedImage(msg.what); 
       //imageAdapter.notifyDataSetChanged(); 
       break; 
     } 
    } 
};