tomiQrsd
tomiQrsd

Reputation: 119

Using Bitmap ArrayList to dynamically populate ListView

I'm new to android so please be tolerant to my lack of knowledge. My app is taking photos and saving them to Bitmap ArrayList, it works, i tested it, photos are displayable using ImageViews. Everything responsible for taking pictures looks like this:

//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
    }
}

I also have ListView, i'm extending ListActivity instead of just Activity becouse i found that working solution for dynamic String ArrayList in web. XML declaration of ListView: (with that method i don't have to declare it in the Java code)

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

I also use Bitmap adapter:

ArrayAdapter<Bitmap> adapter;

and then initialize it inside of onCreate method:

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

item.xml is the problem i have. I don't know how to create working ListView item that contains ImageView capable of displaying my Bitmap ArrayList elements preferably on the left. I tried adding just a ImageView but app crashes after saving the photo. My go-to functionality is having photo on the left, short description in the middle and ratingbar on the right, clicking on item takes user to another Activity where one can modify the description and rating, also displaying photo in full size. Some kind of hint will go a long way! Thank you in advance!

Upvotes: 0

Views: 1129

Answers (1)

VadymVL
VadymVL

Reputation: 5566

You should implement a custom adapter, to work with your images, and inflate your list. You can find useful tutorial here, which describes how to use listViews and adapters, and how to create your own list items.

You can create your custom XML item layout like usual layout:

<?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> 

But for adding images to it, you must implement your own custom adapter.

Upvotes: 1

Related Questions