Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用设备上的图像填充listView_Android_Listview_Android Layout_Android Sdcard - Fatal编程技术网

Android 使用设备上的图像填充listView

Android 使用设备上的图像填充listView,android,listview,android-layout,android-sdcard,Android,Listview,Android Layout,Android Sdcard,好的,我花了两天的时间寻找一个简单的例子,说明如何使用设备上的图像填充列表视图,我得出结论,没有简单的方法可以做到这一点。我知道,每次我把别人的一些片段放在一起,我通常会得到很多我不需要的额外代码。有人能告诉我如何简单地将图像从设备加载到列表视图吗。这里没有一本书、教程或帖子只是简单地展示了如何做到这一点,同时也展示了它的有趣和疯狂。下面是代码的其余部分,使用上面链接的SDImageLoader: ListAdapter类: public class PBListAdapter extends

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

下面是代码的其余部分,使用上面链接的SDImageLoader:

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));
}

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

到目前为止,我在网上找到的唯一有意义的部分是我需要的代码的图像收集部分,但是我不知道如何实现它,因此,您只想在设备上存储的listview中显示图像。是的,请确认。您可以参考此Hi Anup:)是的,我想从设备中提取图像以在列表视图中查看它们。一旦我走到这一步,我肯定我能把onclick和其他我想用的东西都设置好:)Garcon你就是那个人:)我要试试这个,看看它是否管用Hey Garcon…..我试着用你的例子从我的sd卡上获取图像,但我很难弄清楚到底用什么来代替你的SpecimenHunterDatabaseAdapter mDbHelper。我不认为我需要一个数据库来在列表视图中使用sd卡上的图像……是吗?你可以去掉那个部分。只有这样,我才能使用fetchSpeciesName()——在这种情况下,可能有更有效的方法来实现这一点,但我对这个项目的SQL非常陌生,它并不会让应用程序变慢:)