效果图:

第一步:修改main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip" > <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>

第二步:编写list.xml 用以在main.xml中的ListView中显示数据:

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip"> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="@+id/ItemTitle" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="15dip"/> <TextView android:text="@+id/ItemText" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="15dip"/> </TableRow> </TableLayout>

注:本例中使用一个TableLayout来显示想在ListView中显示的内容

第三步:准备数据,并且有效的使这两个xml文件组合起来:

package com.listview; 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 _ListView extends Activity { private List<Map<String, String>> list; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 绑定main.xml中的ListView,作为Item的容器 ListView lview = (ListView) findViewById(R.id.ListView01); // 生成动态数组,并且转载数据 //每一次for循环 产生一行两列的数据 list = new ArrayList<Map<String, String>>(); for (int i = 0; i < 10; i++) { Map<String, String> map = new HashMap<String,String>(); map.put("key", "key " + Integer.toString(i)); map.put("value", "value " + Integer.toString(i)); list.add(map); } // 生成适配器,数组===》ListItem SimpleAdapter adapter = new SimpleAdapter( this, // 当前Context list, // 数据来源 R.layout.list, // ListItem的实现 new String[] {"key","value"}, // 动态数组与ListItem对应的子项 new int[] { R.id.ItemTitle, R.id.ItemText } // ListItem的xml文件里的两个TextView ); lview.setAdapter(adapter); } }

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. 错误总结1,动态加载部分不能显示的原因
  3. android 数据存储 SharePreferences 简单使用
  4. Android中EditText输入字数统计和限制
  5. Android(安卓)中的显示单位
  6. 使用SQLiteOpenHelper 和使用ContentProvider。
  7. Android请求数据格式与Java后台需求格式不匹配HttpMediaTypeNotS
  8. Android(安卓)Studio导入项目app module一直不显示
  9. android仿微信实现欢迎界面、介绍界面及单击按钮跳转到主界面

随机推荐

  1. Android完美解决输入框EditText隐藏密码
  2. Android中Activity组件详解
  3. Android4.2增加新键值
  4. 浅谈android的selector背景选择器
  5. 用Visual Studio 2010开发Android应用
  6. [导入]Android植入Wooboo广告教程
  7. Android(安卓)Animation学习笔记
  8. android:shrinkColumns的用法 (2011-04-2
  9. android的PowerManager和PowerManager.Wa
  10. Android(安卓)requires compiler complia