使用位图ArrayList动态填充ListView

Using Bitmap ArrayList to dynamically populate ListView

我是android的新手,所以请原谅我的知识不足。 我的应用程序正在拍摄照片并将它们保存到位图 ArrayList,它工作正常,我测试了它,照片可以使用 ImageViews 显示。一切负责拍照的都是这样的:

//declaration
List<Bitmap> bmpList = new ArrayList<Bitmap>();

//onClick method of button for takin pics
public void takePic(View view){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 0){
        Bitmap image = (Bitmap) data.getExtras().get("data");
        bmpList.add(image);
        adapter.notifyDataSetChanged(); //more about my adapter and stuff below
    }
}

我也有 ListView,我正在扩展 ListActivity 而不仅仅是 Activity 因为我发现了 web 中动态字符串 ArrayList 的工作解决方案。 XML ListView 的声明:(使用该方法我不必在 Java 代码中声明它)

<ListView android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/photoButton" 
android:id="@android:id/list"> 
</ListView>

我也使用位图适配器:

ArrayAdapter<Bitmap> adapter;

然后在onCreate方法中初始化它:

adapter = new ArrayAdapter<Bitmap>(this, R.layout.item, bmpList);
setListAdapter(adapter);

item.xml 是我遇到的问题。我不知道如何创建包含 ImageView 的工作 ListView 项目,该项目最好在左侧显示我的位图 ArrayList 元素。我尝试只添加一个 ImageView,但应用程序在保存照片后崩溃。我的首选功能是在左侧显示照片,在中间显示简短描述,在右侧显示评级栏,单击项目会将用户带到另一个 Activity,用户可以在其中修改描述和评级,还可以全尺寸显示照片.某种提示会有很长的路要走!提前致谢!

您应该实现一个自定义适配器,以处理您的图像并扩充您的列表。您可以找到有用的教程 here,它描述了如何使用 listView 和适配器,以及如何创建您自己的列表项。

您可以像通常的布局一样创建自定义 XML 项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 

但是要向其中添加图像,您必须实现自己的 custom adapter