开发者

Image in ListView

Hey friends, I am creating ToDoTask application in android. I wam displaying all the tasks开发者_高级运维 in "ListView". Now i want to add an image in front of each task in listview . This is a small image which shows "done" or "Undone" state of the task.

How can i accomplish this. I have saved all the task list into the database.....


To display your tasks in a ListView you have to overwrite ListView#getView() I assume. Now you're for example returning a TextView, but you can change this and return for example a LinearLayout that first contains an ImageView and then the TextView.


You would have to create a custom list adapter and override its getView() function. Then create a layout of your custom listview item that includes a textview and an imageview. Then you can initialize the textview and imageview in the getView() function of your custom list adapter and change the value of the textview and the image of the imageview however you want.

GetView() function:

public View getView(int position, View convertView, ViewGroup viewGroup)
{
    TextView tv = (TextView)convertView.findViewById(R.id.tvChild);
    tv.setText("example text");
    ImageView image = (ImageView)convertView.findViewById(R.id.image);
    // You can choose to use either of the below functions to set the image
    image.setImageBitmap(null);
    image.setImageDrawable(null);
    image.setImageResource(Position);
}

Custom listview Item layout:

<LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal">
        <ImageView android:id="@+id/image"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:gravity="center_vertical">
        </ImageView>
        <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:id="@+id/tvChild"
                android:layout_marginLeft="6dip"
</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜