使用设备上的图像填充 listView

发布于 2024-11-19 11:49:02 字数 180 浏览 6 评论 0原文

好吧,我花了两天时间寻找一个很好的简单示例,说明如何使用设备上的图像来填充列表视图,我得出的结论是,没有简单的方法可以做到这一点。我知道,每次我将别人示例中的点点滴滴放在一起时,通常都会得到很多我不需要的额外代码。有人可以告诉我如何简单地将图像从设备加载到列表视图吗?这里没有任何书籍、教程或帖子只是简单地展示如何做到这一点,而且它既有趣又疯狂。

Ok so Ive spent the last two days looking for a good simple example of how to use the images on a device to populate a list view and Ive come to the conclusion that there is no easy way to do this. I know that everytime I put together bits and pieces from some one elses examples that I usually end up with lots of extra code i dont need. Can some one please show me how to simply load images from the device to a list view. There is no book, tutorial, or post on here that just simply shows how to do this and its kind of funny and crazy at the same time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-11-26 11:49:02

这是其余的代码,使用上面链接的 SDImageLoader: http://www.samcoles.co.uk/mobile/android-asynchronously-load-image-from-sd-card

ListAdapter 类:

public class PBListAdapter extends SimpleCursorAdapter {

//MEMBERS:

private int mLayoutId;
private SpecimenHunterDatabaseAdapter mDbHelper;
private final SDImageLoader mImageLoader = new SDImageLoader();


//METHODS:

public PBListAdapter(Context context, int layout, Cursor c) {
    super(context, layout, c, new String[] {}, new int[] {});
    mLayoutId = layout; 
    mDbHelper = new SpecimenHunterDatabaseAdapter(context);
    mDbHelper.open();
}

@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(mLayoutId, parent, false);
    return v;               
}

@Override
public void bindView(View v, Context context, Cursor c) {   

    String title = c.getString(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_TITLE));
    String species = mDbHelper.fetchSpeciesName(c.getInt(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_SPECIES)));
    int pounds = c.getInt((c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_POUNDS)));
    int ounces = c.getInt((c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_OUNCES)));
    int drams = c.getInt((c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_DRAMS)));  
    String weight = pounds + context.getString(R.string.addcapture_pounds) + " " +
                    ounces + context.getString(R.string.addcapture_ounces) + " " + 
                    drams + context.getString(R.string.addcapture_drams);
    String photoFilePath = c.getString(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_PHOTO));
    String comment = c.getString(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_COMMENT));

    TextView nameView = (TextView)v.findViewById(R.id.pb_row_species_name);
    TextView weightView = (TextView)v.findViewById(R.id.pb_row_capture_weight);
    TextView titleView = (TextView)v.findViewById(R.id.pb_row_capture_title);
    TextView commentView = (TextView)v.findViewById(R.id.pb_row_capture_comment);
    ImageView photoView = (ImageView)v.findViewById(R.id.pb_row_capture_photo);

    mImageLoader.load(context, photoFilePath, photoView);       
    nameView.setText(species);
    titleView.setText(title);
    weightView.setText(weight);
    commentView.setText(comment);
}
}

在实际的 ListActivity 中,在 onCreate() 中设置列表适配器:

private void bindData() {       
    Cursor c = mDbHelper.fetchAllPBs();
    startManagingCursor(c);     
    setListAdapter(new PBListAdapter(this, R.layout.pb_list_item, c));
}

在我的示例中,通过应用程序添加图像时,图像的绝对文件路径存储在数据库中。

Here's the rest of the code, using the SDImageLoader linked to above from here: http://www.samcoles.co.uk/mobile/android-asynchronously-load-image-from-sd-card

The ListAdapter class:

public class PBListAdapter extends SimpleCursorAdapter {

//MEMBERS:

private int mLayoutId;
private SpecimenHunterDatabaseAdapter mDbHelper;
private final SDImageLoader mImageLoader = new SDImageLoader();


//METHODS:

public PBListAdapter(Context context, int layout, Cursor c) {
    super(context, layout, c, new String[] {}, new int[] {});
    mLayoutId = layout; 
    mDbHelper = new SpecimenHunterDatabaseAdapter(context);
    mDbHelper.open();
}

@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(mLayoutId, parent, false);
    return v;               
}

@Override
public void bindView(View v, Context context, Cursor c) {   

    String title = c.getString(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_TITLE));
    String species = mDbHelper.fetchSpeciesName(c.getInt(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_SPECIES)));
    int pounds = c.getInt((c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_POUNDS)));
    int ounces = c.getInt((c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_OUNCES)));
    int drams = c.getInt((c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_DRAMS)));  
    String weight = pounds + context.getString(R.string.addcapture_pounds) + " " +
                    ounces + context.getString(R.string.addcapture_ounces) + " " + 
                    drams + context.getString(R.string.addcapture_drams);
    String photoFilePath = c.getString(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_PHOTO));
    String comment = c.getString(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_CAPTURES_COMMENT));

    TextView nameView = (TextView)v.findViewById(R.id.pb_row_species_name);
    TextView weightView = (TextView)v.findViewById(R.id.pb_row_capture_weight);
    TextView titleView = (TextView)v.findViewById(R.id.pb_row_capture_title);
    TextView commentView = (TextView)v.findViewById(R.id.pb_row_capture_comment);
    ImageView photoView = (ImageView)v.findViewById(R.id.pb_row_capture_photo);

    mImageLoader.load(context, photoFilePath, photoView);       
    nameView.setText(species);
    titleView.setText(title);
    weightView.setText(weight);
    commentView.setText(comment);
}
}

In the actual ListActivity, in your onCreate() set the list adapter:

private void bindData() {       
    Cursor c = mDbHelper.fetchAllPBs();
    startManagingCursor(c);     
    setListAdapter(new PBListAdapter(this, R.layout.pb_list_item, c));
}

In my example the absolute filepaths for the images are stored in the database when added via the app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文