0
我試圖通過從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;
}
}
對不起,我是新手編程,所以你可以給你一個什麼樣的例子嗎?所以在這種情況下,我會把公共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; }但是,整數數組的mImageIds呢? – maxchia