在图库中使用延迟列表

时间:2012-09-01 06:15:51

标签: android android-gallery

我正在尝试通过从sqlite数据库中提取图片名称然后使用该图片名称来查找可绘制文件夹中的特定图片并将其显示在我的应用程序中的图库小部件中来显示一些图片。它的工作原理但现在的问题是我有太多的图片,因此将所有内容存储在drawable文件夹中是没有意义的,因为它会使应用程序太大。我正在考虑将图片放在网上并将个人网址存储到sqlite数据库中。通过这种方式,我可以从数据库中获取URL并使用该URL,我可以下载图像并将其显示在库窗口小部件中。我读到了懒惰列表(对于伟大的工作的荣誉!)但我在我的应用程序中实现了问题。下面是我用于图库的当前代码,我不太清楚如何修改它以利用Lazy List从在线下载图像。任何帮助是极大的赞赏! =)

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

1 个答案:

答案 0 :(得分:0)

使用Lazylist编码中的ImageLoader(),并将ImageView and Imageurl传递给ImageLoader 像这样

imageLoader.DisplayImage(ImageUrl, imageview);

在适配器getView()方法中。