---
title: "常用组件-ListView"
author: "DanielHan"
date: "2014-07-07T15:23:49Z"
tags: []
categories: ["移动开发"]
slug: "listview"
draft: false
lastmod: "2014-07-07T15:27:26Z"
id: 2089066
url: "https://danielhan.iteye.com/blog/2089066"
---

**用法一**
data.xml

```xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    >
    <ImageView 
             android:id="@+id/image"
             android:layout_width="wrap_content" 
             android:layout_height="fill_parent"
             android:layout_alignParentTop="true" 
             android:layout_alignParentBottom="true"
             android:adjustViewBounds="true"
             />
       
     <TextView 
              android:id="@+id/text1"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
               android:layout_toRightOf="@id/image"
              android:textSize="14px" />

     <TextView       
            android:id="@+id/text2"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:layout_below="@id/text1"
            android:textSize="22px" 
            />
</RelativeLayout>

```


activity_main.xml

```xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>
	<TextView 
	    android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="菜单"
    	android:textSize="20px"
    	android:gravity="center"
    	android:background="#4E87C4"/>
	<ListView 
	    android:id="@+id/list"
	    android:layout_width="fill_parent"
    	android:layout_height="wrap_content"/>
</LinearLayout>

```


MainActivity.java

```java

package com.example.listviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	private ListView list=null;
	private static final String[] food = { "猪肉", "猪肝", "猪血", "羊肉", "牛肉", "牛肝", "鹅肉", "兔肉", "狗肉",
		"鸭肉", "鸡肉", "驴肉", "鸡蛋", "鲤鱼", "黄鱼", "虾", "虾皮", "螃蟹", "蛤", "鳖肉",
		"田螺", "大蒜", "葱", "萝卜", "芹菜", "韭菜", "菠菜", "莴笋", "竹笋", "西红柿", "洋葱",
		"醋", "茶", "豆浆", "红糖", "蜂蜜", "牛奶", "白酒", "啤酒" };
	private static final String[] food1 = { "黄莲", "荞麦 雀肉 豆芽", "何首乌 地黄 黄豆 海带", "醋 红豆 半夏 南瓜",
		"橄榄 板粟 韭菜 ", "鲇鱼 鳗鱼 柿子", "狗肉 鲤鱼 柑橘", "鲤鱼 绿豆", "鳖", "鲤鱼", "金针菇",
		"豆浆 兔肉", "甘草 麦冬", "荞麦面 ", "富含维生素C的食物", "红枣 黄豆",
		"梨 柿子 茄子 花生仁 石榴 香瓜 芹菜 蜂蜜 西红柿", "芹菜 ", "鸭肉", "香瓜 木耳 牛肉 蚕豆 玉米",
		"地黄 何首乌 白术", "枣", "橘子 木耳", "黄瓜 蚬、蛤、蟹", "牛肉", "豆腐 鳝鱼 黄瓜", "蜂蜜",
		"糖浆", "白酒", "蜂蜜", "胡萝卜", "酒", "蜂蜜", "竹笋", "皮蛋", "豆腐 韭菜",
		"钙片果汁  药物 韭菜 柠檬", "胡萝卜 核桃 啤酒 红薯", "海鲜" };
	private static final int[] images = { R.drawable.pork, R.drawable.pigliver, R.drawable.pigblood,
		R.drawable.lamb, R.drawable.beef, R.drawable.beefliver,
		R.drawable.goose, R.drawable.rabbit, R.drawable.dog,
		R.drawable.duck, R.drawable.chicken, R.drawable.donkey,
		R.drawable.egg, R.drawable.carp, R.drawable.yellowfish,
		R.drawable.shrimp, R.drawable.shrimp2, R.drawable.crab,
		R.drawable.clam, R.drawable.turtle, R.drawable.riversnail,
		R.drawable.garlic, R.drawable.onion, R.drawable.radish,
		R.drawable.celery, R.drawable.leek, R.drawable.spinach,
		R.drawable.lettuce, R.drawable.bamboo, R.drawable.tomato,
		R.drawable.foreignonion, R.drawable.vinegar, R.drawable.tea,
		R.drawable.beanmilk, R.drawable.brownsuger, R.drawable.honey,
		R.drawable.milk, R.drawable.whitespirit, R.drawable.beer };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		list=(ListView)findViewById(R.id.list);
		
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < food.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("image", images[i]);
			map.put("text1", food[i]);
			map.put("text2", food1[i]);
            lists.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(this, lists,
				R.layout.data, new String[] { "image",
						"text1", "text2" }, new int[] {
						R.id.image, R.id.text1, R.id.text2 });
		list.setAdapter(adapter);
	}
}

```


**用法二**
data.xml
与用法一一样

activity_main.xml
![](http://dl2.iteye.com/upload/attachment/0098/8444/539f1e1a-474c-365c-a6d1-b9c32a063e69.png)

MainActivity.java

![](http://dl2.iteye.com/upload/attachment/0098/8449/55a27097-3f6e-3b13-b59d-66cd4b3551b0.png)

![](http://dl2.iteye.com/upload/attachment/0098/8451/380a5c13-3dcf-3480-ba12-56af8195feef.png)

**结果**

![](http://dl2.iteye.com/upload/attachment/0098/8519/ec3dff4f-fdc1-3034-a0a1-4c5cbc5cfee2.png)