获取图像从AsyncTask的自定义的GridView适配器 [英] Getting image from asynctask to custom Gridview Adapter

查看:112
本文介绍了获取图像从AsyncTask的自定义的GridView适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个画廊的应用程序,当我只有在的AsyncTask的preExecution一部分执行适配器类的我的AsyncTask在Getview然后得到执行和别人不一样。

i am making a gallery app and when i execute my asynctask in Getview of Adapter class then only onPreExecution part of Asynctask gets execute and others don't.

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

So, what i see is a progress bar But not the image after downloading...on my activity just a progress bar keeps on rolling

下面是Getview方法,我的code

Here is my code of Getview method

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

}

这是我Asynctaskclass

and this is my Asynctaskclass

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();
 }
}    

我想执行的preExecution后,只需右键我的GetView方法返回IMG变量来我MainActivity class..here是

I think Just right after executing onPreExecution my GetView method return img variable to my MainActivity class..here it is

  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);

        }
    });

谁能告诉我如何着手?

Can anybody tell me how to proceed?

推荐答案

您不应该运行的AsyncTask getView()<你的/ code>。既然是异步的, getView()不会等待其返回。它会返回一个空的ImageView

You shouldn't be running your AsyncTask in getView(). Since it is asynchronous, getView() won't wait for it to return. It will return a blank ImageView.

您应该设置适配器之前运行任务,并得到所有图像的第一位。

You should run the task before setting the adapter and get all of the images first.

您也可以使用像毕加索一些图像加载库。你可以谷歌和找到其他图书馆,以及,以及如何使用它们。你不应该使用 StrictMode

You can also use some image loading library like Picaso. You can Google and find other libraries, as well, and how to use them. And you shouldn't be using StrictMode.

要在运行之前获得的图像 getView()

To get the images before running getView()

运行在的onCreate任务()。然后,只需设置适配器 onPostExecute()你的任务。

Run the task in onCreate(). Then simply set your Adapter in onPostExecute() of your task.

<一个href="http://stackoverflow.com/questions/21246622/getting-image-from-an-url-the-view-stays-empty/21246743#21246743">Related解释

这篇关于获取图像从AsyncTask的自定义的GridView适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆