2015-03-19 97 views
0

我是新来的机器人,请容忍我缺乏知识。 我的应用程序正在拍照并将它们保存到位图ArrayList,它的工作原理,我测试它,照片可以使用ImageViews显示。一切负责拍照看起来是这样的:使用位图ArrayList动态填充ListView

//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而不是只活动becouse我发现在网络动态字符串的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,但保存照片后应用程序崩溃。我的前往功能是在左边有照片,中间是简短描述,右边是评级栏,点击项目会将用户带到另一个活动,在那里可以修改描述和评分,同时也可以显示全尺寸的照片。某种暗示会很长一段时间!先谢谢你!

回答

1

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

您可以创建自定义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

+0

我有一些麻烦创建自定义适配器,以便它可以处理位图ArrayList – tomiQrsd 2015-03-19 12:32:45

+0

您可能会发现有用的相关答案 - [this](http://stackoverflow.com/questions/5070830/populating-a-listview- using-arraylist?rq = 1)和[this](http://stackoverflow.com/questions/157944/create-arraylist-arraylistt-from-array-t?rq=1)。如果没有帮助,在这里发帖,你有什么麻烦。 – VadymVL 2015-03-19 12:35:34

+0

谢谢,我设法创建了自定义适配器 – tomiQrsd 2015-03-19 22:39:48