I have written a program for viewpager to display images using pageradapter but I am experiencing a problem, as there are only two images that I am adding it to the list and returning the list.
I added a println to see whether the images are called properly or not and they are getting called but it is getting called twice.
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....
Your constructor and AsyncTask are called twice because
public Object instantiateItem(ViewGroup container, int position)
method is called twice.
How many times the above method is called depend on the return value of
public int getCount()
method. In your case you are returning imgURLS.length
which is 2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.