使用Lazy List的Gallery小部件

时间:2012-09-02 16:24:50

标签: android android-widget lazy-loading

对于我正在尝试编写的这个应用程序,我的想法是,我将在线托管一些图片并将个人网址保存到sqlite数据库中。然后我将从数据库中提取每个URL并下载要在库窗口小部件中显示的图片。我读到了懒惰列表(对于伟大的工作而言的荣誉!)但是我在实现它时遇到了问题。我试图从Lazy List修改编码,但它似乎没有用。我不确定我的应用程序中是否有错误,或者我是否错误地修改了Lazy List。非常感谢任何帮助,并提前感谢您! =)

我的应用代码:

public class ResultDetails extends ListActivity {

    protected int foodId;
    protected String pic1, pic2, pic3, pic4, pic5;
    LazyAdapter adapter1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_details);
        foodId = getIntent().getIntExtra("FOOD_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, pic1, pic2, pic3, pic4, pic5 FROM database WHERE _id = ?", new String[]{""+foodId});
    pic1 = cursor.getString(cursor.getColumnIndex("pic1"));
    pic2 = cursor.getString(cursor.getColumnIndex("pic2"));
    pic3 = cursor.getString(cursor.getColumnIndex("pic3"));
    pic4 = cursor.getString(cursor.getColumnIndex("pic4"));
    pic5 = cursor.getString(cursor.getColumnIndex("pic5"));
    String[] mStrings={
            pic1,
            pic2,
            pic3, 
            pic4, 
            pic5};
    Gallery g = (Gallery) findViewById(R.id.photobar);
    adapter1=new LazyAdapter(this, mStrings);
    g.setAdapter(adapter1);
}

我已经修改了LazyAdapter:

public class LazyAdapter extends BaseAdapter {
   int mGalleryItemBackground;
   private Context mContext;

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }
    public LazyAdapter(Context c) {
       mContext = c;
       TypedArray b = c.obtainStyledAttributes(R.styleable.Theme);
       mGalleryItemBackground = b.getResourceId(
         R.styleable.Theme_android_galleryItemBackground,
                   0);
       b.recycle();
   }


    public int getCount() {
        return data.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 vi = new ImageView(mContext);
        imageLoader.DisplayImage(data[position], vi);
        vi.setLayoutParams(new Gallery.LayoutParams(150, 100));
    vi.setScaleType(ImageView.ScaleType.FIT_XY);
        return vi;
    }
}

1 个答案:

答案 0 :(得分:0)

在Fedor的帮助下,我发现了实施的问题。无论是列表还是图库,都无需更改LazyAdapter!只需按原样使用适配器,然后在应用中使用以下代码加载图片:

 g = (Gallery) findViewById(R.id.photobar);
 adapter1=new LazyAdapter(this, mStrings);
 g.setAdapter(adapter1);}

我注意到有很多关于将LazyList用于列表而不是库的示例。为了帮助那些想要将它用于画廊的人,这里有一个实现的例子:

public class ResultDetails extends ListActivity {

protected int foodId;
protected String pic1, pic2, pic3, pic4, pic5;
LazyAdapter adapter1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_details);
    foodId = getIntent().getIntExtra("FOOD_ID", 0);
    SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT _id, pic1, pic2, pic3, pic4, pic5 FROM database WHERE _id = ?", new String[]{""+foodId});
    pic1 = cursor.getString(cursor.getColumnIndex("pic1"));
    pic2 = cursor.getString(cursor.getColumnIndex("pic2"));
    pic3 = cursor.getString(cursor.getColumnIndex("pic3"));
    pic4 = cursor.getString(cursor.getColumnIndex("pic4"));
    pic5 = cursor.getString(cursor.getColumnIndex("pic5"));
    String[] mStrings={pic1, pic2, pic3, pic4, pic5};
    g = (Gallery) findViewById(R.id.photobar);
    adapter1=new LazyAdapter(this, mStrings);
    g.setAdapter(adapter1);}