2015-03-19 93 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