簡體   English   中英

使用位圖ArrayList動態填充ListView

[英]Using Bitmap ArrayList to dynamically populate ListView

我是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,但保存照片后應用程序崩潰。 我的首選功能是在左側顯示照片,在中間顯示簡短說明,在右邊的評級欄,單擊該項目可將用戶帶到另一個活動,在該活動中,用戶可以修改描述和評級,並以全尺寸顯示照片。 某種暗示會走很長一段路! 先感謝您!

您應該實現一個自定義適配器,以處理圖像並增加列表。 您可以在此處找到有用的教程,該教程描述了如何使用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> 

但是要向其中添加圖像,必須實現自己的自定義適配器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM