【问题标题】:Using Bitmap ArrayList to dynamically populate ListView使用 Bitmap ArrayList 动态填充 ListView
【发布时间】:2015-03-19 11:49:08
【问题描述】:

我是 android 新手,所以请容忍我缺乏知识。 我的应用程序正在拍摄照片并将它们保存到 Bitmap 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 中动态 String ArrayList 的工作解决方案。 ListView 的 XML 声明:(使用该方法我不必在 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 项目,该项目最好在左侧显示我的 Bitmap ArrayList 元素。我尝试只添加一个 ImageView 但保存照片后应用程序崩溃。我的首选功能是左侧有照片,中间有简短描述,右侧有评分栏,单击项目会将用户带到另一个可以修改描述和评分的活动,也可以全尺寸显示照片。某种提示会有很长的路要走!提前谢谢!

【问题讨论】:

    标签: java android listview arraylist bitmap


    【解决方案1】:

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

    您可以像通常的布局一样创建自定义 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

    【讨论】:

    • 我在创建自定义适配器时遇到了一些麻烦,因此它可以处理位图的 ArrayList
    • 您可能会找到有用的相关答案 - thisthis。如果没有帮助,请在此处发布,您有什么麻烦。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多