当前位置: 动力学知识库 > 问答 > 编程问答 >

android - populating a listView with images on the device

问题描述:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: