使用设备上的图像填充listView

时间:2011-07-12 07:52:11

标签: android listview android-layout android-sdcard

好的,所以我花了最后两天寻找一个很好的简单示例,说明如何使用设备上的图像来填充列表视图,我得出结论,没有简单的方法来做到这一点。我知道,每当我把一些零件和一些零件放在一起时,我通常最终会得到许多额外的代码,我不需要。有人可以告诉我如何简单地将图像从设备加载到列表视图。这里没有任何书籍,教程或帖子,只是简单地说明了如何做到这一点以及同时又有趣和疯狂。

1 个答案:

答案 0 :(得分:1)

以下是其余代码,使用上面链接到上面的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));
}

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