【问题标题】:android issue with populating the list item in a ListView在 ListView 中填充列表项的 android 问题
【发布时间】:2011-11-22 10:10:50
【问题描述】:

在我的应用程序中,要填充ListView,我使用的是自定义适配器,因为一个listitem 包含3 个TextViews 和1 个ImageView。 每次从 url 获取图像。

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

所以我希望没有图像列表应该首先填充只有Textviews,然后只有图像应该出现。

我该怎么做?

【问题讨论】:

标签: android listview


【解决方案1】:

通过使用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);
    }
}

【讨论】:

  • 我必须在自定义的adaoter write中写这些东西?
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    https://github.com/thest1/LazyList

    Lazy load of images in ListView

    【讨论】:

      【解决方案4】:

      基本理念是在您的应用中已有加载图像。 然后使用 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;
              }
          }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多