0

I'm trying to display some pictures by extracting the picture name from a sqlite database and then using that picture name to find that particular picture in the drawable folder and display it in a gallery widget in my app. It works but now the problem is that I have too many pictures, so it doesn't make sense to store everything in the drawable folder as it will make the app too big. I am thinking of placing the pictures online and store the individual url into the sqlite database instead. In this way, I can get the url from the database and using that url, I can download the image and display it in the gallery widget. I read about Lazy List (kudos for the great work!) but I'm having problem implementing in my app. Below is the current code I am using for the gallery and I'm not too sure how to modify it to make use of Lazy List to download the images from online. Any help is greatly appreciated! =)

protected String pic1, pic2, pic3;
protected int Id, resID1, resID2, resID3;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_details);
    Id = getIntent().getIntExtra("ID", 0);
    SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT pic1, pic2, pic3 FROM database WHERE _id = ?", 
            new String[]{""+Id});
    pic1 = cursor.getString(cursor.getColumnIndex("pic1"));
    pic2 = cursor.getString(cursor.getColumnIndex("pic2"));
    pic3 = cursor.getString(cursor.getColumnIndex("pic3"));

    resID1 = getResources().getIdentifier(pic1 , "drawable", getPackageName());
resID2 = getResources().getIdentifier(pic2 , "drawable", getPackageName());
    resID3 = getResources().getIdentifier(pic3 , "drawable", getPackageName());

    Gallery g = (Gallery) findViewById(R.id.photobar);
    g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
       int mGalleryItemBackground;
       private Context mContext;

       private Integer[] mImageIds = {
               resID1,
               resID2,
               resID3
       };

       public ImageAdapter(Context c) {
           mContext = c;
           TypedArray a = obtainStyledAttributes(R.styleable.Theme);
           mGalleryItemBackground = a.getResourceId(
             R.styleable.Theme_android_galleryItemBackground,
                       0);
           a.recycle();
       }

       public int getCount() {
           return mImageIds.length;
       }

       public Object getItem(int position) {
           return position;
       }

       public long getItemId(int position) {
           return position;
       }

       public View getView(int position,
           View convertView, ViewGroup parent) {
           ImageView i = new ImageView(mContext);

           i.setImageResource(mImageIds[position]);
           i.setLayoutParams(new Gallery.LayoutParams(150, 100));
           i.setScaleType(ImageView.ScaleType.FIT_XY);

           return i;
       }
    }
maxchia
  • 23
  • 1
  • 6

1 Answers1

0

Use ImageLoader() from the Lazylist coding, and pass ImageView and Imageurl to ImageLoader like this

imageLoader.DisplayImage(ImageUrl, imageview);

in the adapter getView() method.

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • Sorry, I'm new to programming so can you give an example of what you said? So in this case, I will put public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); imageLoader.DisplayImage(pic1, i); i.setImageResource(mImageIds[position]); i.setLayoutParams(new Gallery.LayoutParams(150, 100)); i.setScaleType(ImageView.ScaleType.FIT_XY); return i; } But what about the integer array of mImageIds? – maxchia Sep 02 '12 at 01:33