[b][size=x-large]用法一[/size][/b]
[size=large]data.xml[/size]
[size=large]activity_main.xml[/size]
[size=large]MainActivity.java[/size]
[b][size=x-large]用法二[/size][/b]
[size=large]data.xml[/size]
与用法一一样
[size=large]activity_main.xml[/size]
[img]http://dl2.iteye.com/upload/attachment/0098/8444/539f1e1a-474c-365c-a6d1-b9c32a063e69.png[/img]
[size=large]MainActivity.java[/size]
[img]http://dl2.iteye.com/upload/attachment/0098/8449/55a27097-3f6e-3b13-b59d-66cd4b3551b0.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0098/8451/380a5c13-3dcf-3480-ba12-56af8195feef.png[/img]
[b][size=x-large]结果[/size][/b]
[img]http://dl2.iteye.com/upload/attachment/0098/8519/ec3dff4f-fdc1-3034-a0a1-4c5cbc5cfee2.png[/img]
[size=large]data.xml[/size]
<?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>
[size=large]activity_main.xml[/size]
<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>
[size=large]MainActivity.java[/size]
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);
}
}
[b][size=x-large]用法二[/size][/b]
[size=large]data.xml[/size]
与用法一一样
[size=large]activity_main.xml[/size]
[img]http://dl2.iteye.com/upload/attachment/0098/8444/539f1e1a-474c-365c-a6d1-b9c32a063e69.png[/img]
[size=large]MainActivity.java[/size]
[img]http://dl2.iteye.com/upload/attachment/0098/8449/55a27097-3f6e-3b13-b59d-66cd4b3551b0.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0098/8451/380a5c13-3dcf-3480-ba12-56af8195feef.png[/img]
[b][size=x-large]结果[/size][/b]
[img]http://dl2.iteye.com/upload/attachment/0098/8519/ec3dff4f-fdc1-3034-a0a1-4c5cbc5cfee2.png[/img]
Android ListView 示例
本文通过一个具体实例展示了如何在Android应用中使用ListView组件显示带有图片和文字的数据列表,并提供了完整的XML布局文件及Java代码。
782

被折叠的 条评论
为什么被折叠?



