我试图通过从 sqlite 数据库中提取图片名称来显示一些图片,然后使用该图片名称在可绘制文件夹中找到该特定图片并将其显示在我的应用程序的图库小部件中。它可以工作,但现在问题是我有太多图片,所以将所有内容存储在可绘制文件夹中没有意义,因为它会使应用程序太大。我正在考虑将图片放在网上,并将单个 url 存储到 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;
}
}