我的Asynctask类中的Imageview被调用两次

时间:2015-02-26 07:04:29

标签: java android constructor android-asynctask

我已经为viewpager编写了一个使用pageradapter显示图像的程序,但是我遇到了一个问题,因为只有两个图像我将它添加到列表并返回列表。

我添加了一个println来查看图像是否被正确调用,并且它们被调用但是它被调用了两次。

public class CustomPagerAdapter extends PagerAdapter {

Context mContext;
LayoutInflater mLayoutInflater;
public ImageView imageView;

String[] imgURLS = { "http://www.picture-newsletter.com/arctic/arctic-
         18.jpg",
        "http://www.picture-newsletter.com/arctic/arctic-20.jpg" };

public CustomPagerAdapter(Context context) {
    mContext = context;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imgURLS.length;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0 == ((RelativeLayout) arg1);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    mLayoutInflater = (LayoutInflater)  
    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, 
    false);
    imageView = (ImageView) itemView.findViewById(R.id.imageView);
    new LoadImage(imageView).execute();
    container.addView(itemView);
    return itemView;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout) object);
}

}

class LoadImage extends AsyncTask<String, String, List<Bitmap>> {

Bitmap bm = null;
List<Bitmap> bmList = new ArrayList<Bitmap>();
ImageView imageView;

public LoadImage(ImageView imageView) {
    System.out.println("LoadImage constructor called...");
    this.imageView = imageView;
}

@Override
protected List<Bitmap> doInBackground(String... params) {
    // TODO Auto-generated method stub
    URL imgUrl = null;
    InputStream is = null;
    HttpURLConnection conn = null;

    try {
        imgUrl = new URL("http://www.picture-newsletter.com/arctic/arctic-
        18.jpg");
        conn = (HttpURLConnection) imgUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
        bm = BitmapFactory.decodeStream(is);// Convert to bitmap
        bmList.add(bm);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        imgUrl = new URL("http://www.picture-newsletter.com/arctic/arctic-
        20.jpg");
        conn = (HttpURLConnection) imgUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
        bm = BitmapFactory.decodeStream(is);// Convert to bitmap
        bmList.add(bm);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bmList;
}

protected void onPostExecute(List<Bitmap> bmList) {
    for (Bitmap bitmap : bmList) {
        System.out.println("Adding bm to imageView....");
        imageView.setImageBitmap(bitmap);
    }
    super.onPostExecute(bmList);
}

MainActivity.java

public class MainActivity extends ActionBarActivity {

ViewPager viewPager;
 CustomPagerAdapter adapter;
//CustomAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.pager);
    adapter = new CustomPagerAdapter(getBaseContext());
    viewPager.setAdapter(adapter);
}
}

LogCat :The constructor is also getting called twice , so is the images

02-26 12:33:06.618: I/System.out(12363): LoadImage constructor called...
02-26 12:33:06.618: I/System.out(12363): LoadImage constructor called...


02-26 12:33:09.363: I/dalvikvm-heap(12363): Grow heap (frag case) to 12.238MB for 4320016-byte allocation
02-26 12:33:13.638: I/System.out(12363): Adding bm to imageView....
02-26 12:33:13.653: I/System.out(12363): Adding bm to imageView....
02-26 12:33:14.793: I/dalvikvm-heap(12363): Grow heap (frag case) to 12.239MB for 4320016-byte allocation
02-26 12:33:17.773: I/dalvikvm-heap(12363): Grow heap (frag case) to 16.359MB for 4320016-byte allocation
02-26 12:33:19.018: I/System.out(12363): Adding bm to imageView....
02-26 12:33:19.018: I/System.out(12363): Adding bm to imageView....

1 个答案:

答案 0 :(得分:0)

你的构造函数和AsyncTask被调用两次,因为

  

public Object instantiateItem(ViewGroup container, int position)

方法被调用两次。

调用上述方法的次数取决于

返回
  

public int getCount()

方法。在您的情况下,您将返回imgURLS.length,即2。