Search code examples
javaandroidimageviewandroid-viewpageruniversal-image-loader

Universal Image Loader ViewPager how to implement


It's almost 2 months now for me, coding in java, android especially. I came into a problem and I've been struggling for over two days now.

Basically, I have a database full of strings(url). I am trying to build an application where I pass the selected (by my query) Urls into the Universal Image Loader and eventually get a Viewpager with all those images that have been downloaded from the UIL.

I have build my own adapter (in order to use it for the ViewPager) but I don't know how to use the Universal Image Loader and create that ViewPager.

Here is my code: MenuImages.java

public class Menu_images extends Activity {


 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.menu_gallery);

 Bundle map = getIntent().getExtras();
 String rest_name=map.getString("key");
 String area=map.getString("key2");

 loadGalleryImages(rest_name, area);


 }

private void loadGalleryImages(String rest_name, String area) {
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    List<String> images_url = db.getImagesUrl(rest_name, area);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);

    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

}

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {

Context context;


private Integer[] Imgid = {
         R.drawable.gallery_photo_1, R.drawable.gallery_photo_3, R.drawable.gallery_photo_1
};

public MyPagerAdapter(List<String> images_url) {
    // TODO Auto-generated constructor stub
}
public int getCount() {
    return Imgid.length;
}

public Object instantiateItem(View collection, int position) {      


    ImageView img = new ImageView(collection.getContext());


    int resId = 0;
    switch (position) {
    case 0:
        resId = Imgid[0];
        break;
    case 1:
        resId = Imgid[1];
        break;
    case 2:
        resId = Imgid[2];
        break;
    }

    img.setImageResource(resId);
    ((ViewPager) collection).addView(img, 0);
    return img;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}
public Parcelable saveState() {
    return null;
}

}

DataBaseHandler.java

public List<String> getImagesUrl(String rest_name, String area) {
    List<String> images_url = new ArrayList<String>();

     // Select All Query
       String selectQuery = "a query goes here";


       SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursor = db.rawQuery(selectQuery, null);





    // looping through all rows and adding to list
       if (cursor.moveToFirst()) {
           do {
               images_url.add(cursor.getString(cursor.getColumnIndex("_images")));
           } while (cursor.moveToNext());
       }

       // closing connection
        cursor.close();
        db.close();

       return images_url;
}

As you can see, I am getting a List with all the String(urls) from the DataBaseHandler. I actually need to remove the array Interger[] from my MyPagerAdapter and replace it dynamically with the List that I retrieve from my DataBaseHandler (the one with the URLs).

To sum up with, I am trying to make Universal Image Loader use all the URL I pass to it and create a viewpager with those.

I would be eternally grateful, if you could give me a hand with that, cause I am starting to freak out. I have spend many days on this part, searching for hours and haven't found something that suits me.

Thanks in advance and sorry for the long talk, Harris.


Solution

  • Here is a perfect example for you. Download the source code on the upper right corner, and run the application. You can study the source code, it has all you need. It is a bit advance, don't freak out. Post questions if you have any.