自定义ListView的item显示内容
1. CustomListViewItem.java在这个文件里设置了ListView的适配器以及item点击监听package com.cn.itcast;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.AdapterView.OnItemClickListener;public class CustomListViewItem extends Activity {/** Called when the activity is first created. */ListView lv;private static final String[] autoStr = new String[] { "CUSTOM NAME","CUSTOM NAME", "CUSTOM NAME", "CUSTOM NAME", "CUSTOM NAME","CUSTOM NAME" };ArrayAdapter<String> arrayadapter;ImageView iv;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.autocomplete);lv = (ListView) findViewById(R.id.showList);// 绑定Layout里面的ListView生成动态数组,加入数据ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < 6; i++) {// Keys and values can be any objectsHashMap<String, Object> map = new HashMap<String, Object>();map.put("ItemImage", R.drawable.icon);// 图像资源的IDmap.put("ItemTitle", autoStr+i);map.put("ItemText", "Department of posts");map.put("ItemText1", "先生");listItem.add(map);}// 生成适配器的Item和动态数组的元素SimpleAdapter liSimpleAdapter = new SimpleAdapter(this, listItem,R.layout.auto_list_item, new String[] { "ItemImage","ItemTitle", "ItemText", "ItemText1" }, new int[] {R.id.ItemImage, R.id.ItemName, R.id.ItemPhone,R.id.ItemText1 });// 添加并且显示lv.setAdapter(liSimpleAdapter);// 添加点击lv.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {setTitle("点击第" + arg2 + "个项目");}});}}
2.autocomplete.xml文件
<?xml version="1.0" encoding="utf-8"?><ListView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/showList" android:layout_width="fill_parent"android:layout_height="fill_parent"></ListView>
3.auto_list_item.xml文件
<?xml version="1.0" encoding="UTF-8"?><RelativeLayout android:id="@+id/RelativeLayout01"android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content" android:paddingBottom="4dip"android:paddingLeft="12dip" android:cacheColorHint="#FFFFFFFF"android:background="@drawable/list_backgroud" android:paddingRight="12dip"><ImageView android:paddingTop="8dip" android:layout_width="wrap_content"android:paddingRight="12dip" android:layout_height="wrap_content"android:id="@+id/ItemImage" /><TextView android:layout_height="wrap_content"android:layout_toRightOf="@id/ItemImage" android:paddingTop="8dip"android:textColor="@color/black" android:textSize="20dip"android:layout_width="wrap_content" android:id="@+id/ItemName" /><TextView android:layout_height="wrap_content"android:layout_toRightOf="@id/ItemImage" android:textColor="@color/black"android:textSize="14dip" android:layout_width="wrap_content"android:layout_below="@+id/ItemName" android:id="@+id/ItemPhone" /><TextView android:layout_toRightOf="@id/ItemName" android:paddingTop="10dp"android:paddingLeft="20dp" android:layout_height="wrap_content"android:layout_width="wrap_content" android:textColor="@color/black"android:id="@+id/ItemText1" /></RelativeLayout>
以上就是自定义item的简单例子
页:
[1]