0

对于我正在尝试编写的这个应用程序,我的想法是我将在线托管一些图片并将各个 url 保存到 sqlite 数据库中。然后我将从数据库中提取每个 url 并下载要在画廊小部件中显示的图片。我读到了 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;
    }
}
4

1 回答 1

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);}
于 2012-09-14T06:49:19.450 回答