用于填充ListView中的列表项的android问题

时间:2011-11-22 10:10:50

标签: android listview

在我的应用程序中,要填充ListView我正在使用自定义适配器,因为一个listitem由3个TextView和1个ImageView组成。 每次从网址获取图片。

因此,当我启动此活动时,需要花费很多时间,因为它会下载所有图像,然后填充列表。

所以我想要没有图片列表应首先填充只有Textview s,然后只有图像应该来。

我该怎么做?

4 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

您可以使用这样的延迟加载图片:

https://github.com/thest1/LazyList

Lazy load of images in ListView

答案 2 :(得分: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]);           
    }
}

}     private void 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;
        }
    }
};

答案 3 :(得分:0)

您必须使用异步任务创建一个惰性图像加载器。

通过这样做,将填充所有列表视图。并且在获取图像时,它们将异步更新到列表视图中。

这是一个链接 - http://iamvijayakumar.blogspot.com/2011/06/android-lazy-image-loader-example.html