【问题标题】:Getting image from asynctask to custom Gridview Adapter从 asynctask 获取图像到自定义 Gridview 适配器
【发布时间】:2014-08-29 13:31:58
【问题描述】:

我正在制作一个画廊应用程序,当我在 Adapter 类的 Getview 中执行我的 asynctask 时,只有 Asynctask 的 onPreExecution 部分得到执行,而其他部分则不执行。

所以,我看到的是一个进度条,但不是下载后的图像...在我的活动中,只是一个进度条不断滚动

这是我的 Getview 方法代码

enter public View getView(int position, View convertview, ViewGroup parent) {
    // TODO Auto-generated method stub
     img=new ImageView(mContext); 
     new DownloadImage().execute();
     return img;

}

这是我的异步任务类

private class DownloadImage extends AsyncTask<String, Void, Bitmap>{
     @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(mContext);
            // Set progressdialog title
            mProgressDialog.setTitle("Download Image");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }
@Override
protected Bitmap doInBackground(String... URL) {
    // TODO Auto-generated method stub
     String imageURL = URL[0];
      Bitmap bitmap = null;
      try {
          // Download Image from URL
          InputStream input = new java.net.URL(imageURL).openStream();
          // Decode Bitmap
          bitmap = BitmapFactory.decodeStream(input);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return bitmap;

}

 @Override
 protected void onPostExecute(Bitmap result) {

     // Set the bitmap into ImageView

     img.setImageBitmap(result);
     // Close progressdialog
     mProgressDialog.dismiss();
 }
}    

我认为就在执行 onPreExecution 之后,我的 GetView 方法将 img 变量返回到我的 MainActivity 类..这里是

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 

    setTitle("My Gallery");
    GridView g=(GridView)findViewById(R.id.gallery);
   // Gallery g=(Gallery)findViewById(R.id.gallery);
  //  ImageAdapter width=new ImageAdapter(context);


    g.setAdapter(new ImageAdapter(this));

    Resources r = getResources();
    float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            GRID_PADDING, r.getDisplayMetrics());





    g.setNumColumns(NUM_OF_COLUMN);
    g.setColumnWidth(columnWidth);
    g.setStretchMode(GridView.NO_STRETCH);
    g.setPadding((int) padding, (int) padding, (int) padding,
            (int) padding);
   // g.setHorizontalSpacing((int) padding);
  //  g.setVerticalSpacing((int) padding);
    g.setOnItemClickListener(new  AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub


            Intent i=new Intent(getApplicationContext(),ImageViewPager.class);
        i.putExtra("id", position);
        startActivity(i);

        }
    });

谁能告诉我如何进行?

【问题讨论】:

    标签: android asynchronous android-asynctask adapter


    【解决方案1】:

    您不应该在getView() 中运行您的AsyncTask。由于它是异步的,getView() 不会等待它返回。它将返回一个空白的ImageView

    您应该在设置适配器之前运行任务并首先获取所有图像。

    您还可以使用一些图像加载库,例如 Picaso。您也可以谷歌并找到其他库,以及如何使用它们。而且你不应该使用StrictMode

    运行前获取图片getView()

    onCreate() 中运行任务。然后只需将您的Adapter 设置为您的任务的onPostExecute()

    Related explanation

    【讨论】:

    • 所以你的意思是说我应该在 MainActivity 中执行 Asynctask 并在获取所有图像之后设置适配器?
    • 是的。根据图像的数量/大小,您可能需要考虑我提到的库。但是,是的,你不想在getView()
    • 这看起来不错,但它要求我在我的 Adapter 类中有一个 DownloadImage 类型的构造函数,我不知道该构造函数将包含什么。实际上我是 android 的新手,所以你的帮助会很重要。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多