免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1011 | 回复: 0

ListView的一个简单实例 [复制链接]

论坛徽章:
0
发表于 2011-12-22 08:51 |显示全部楼层
首先是布局文件
1.main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  8.      android:orientation="vertical"
  9.      android:layout_width="fill_parent"
  10.      android:layout_height="wrap_content"
  11.      >
  12.     
  13.      <ListView
  14.          android:id="@id/android:list"
  15.          android:layout_width="fill_parent"
  16.          android:layout_height="wrap_content"
  17.          android:drawSelectorOnTop="false"
  18.          android:scrollbars="vertical"
  19.          />        
  20.     </LinearLayout>

  21. </LinearLayout>

然后是user.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:padding="10dip"
  7.     >
  8.     <TextView
  9.         android:id="@+id/textUsrName"
  10.         android:layout_width="180dip"
  11.         android:layout_height="30dip"
  12.         android:text="@string/usr_name"
  13.         android:textSize="10pt"
  14.         android:singleLine="true"
  15.         android:gravity="left"
  16.         
  17.     />
  18.     <TextView
  19.         android:id="@+id/textUsrAddr"
  20.         android:layout_width="fill_parent"
  21.         android:layout_height="fill_parent"
  22.         android:text="@string/app_addr"
  23.         android:textSize="10pt"
  24.         android:gravity="right"
  25.         
  26.     />
  27. </LinearLayout>

string.xml中用到的一些变量

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, activity01!</STRING>
  4.     <string name="app_name">listview</string>
  5.     <string name="usr_name">用户名</string>
  6.     <string name="app_addr">IP地址</string>
  7. </resources>

2.再就是java代码

  1. package listview.listview;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;

  4. import android.app.ListActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ListView;
  8. import android.widget.SimpleAdapter;
  9. import android.widget.Toast;

  10. public class activity01 extends ListActivity {
  11.     /** Called when the activity is first created. */
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.       
  17.         ArrayList<HashMap<String,String>> list = new
  18.         ArrayList<HashMap<String,String>>();
  19.         //创建表

  20.         HashMap<String,String> map1 = new HashMap<String,String>();
  21.         HashMap<String,String> map2 = new HashMap<String,String>();
  22.         HashMap<String,String> map3 = new HashMap<String,String>();
  23.         //初始化表数据

  24.         map1.put("textUsrName", "小白兔");
  25.         map1.put("textUsrAddr", "79.3.5.6");
  26.         map2.put("textUsrName", "小黑");
  27.         map2.put("textUsrAddr", "39.3.5.5");
  28.         map3.put("textUsrName", "小红兔子");
  29.         map3.put("textUsrAddr", "16.3.7.6");
  30.         //将表添加进list

  31.         list.add(map1);
  32.         list.add(map2);
  33.         list.add(map3);
  34.         //新建容器adapter

  35.         SimpleAdapter listAdapter = new SimpleAdapter(this,list,R.layout.user,
  36.                 new String[]{"textUsrName","textUsrAddr"},new
  37.                 int []{R.id.textUsrName,R.id.textUsrAddr});
  38.         setListAdapter(listAdapter);
  39.         
  40.         
  41.     }
  42.     
  43.     
  44.     @Override
  45.     protected void onListItemClick(ListView l, View v, int position, long id) {
  46.         // TODO Auto-generated method stub

  47.         super.onListItemClick(l, v, position, id);
  48.         System.out.println("id--------------"+(id+1));
  49.         System.out.println("position--------------"+position);
  50.         Toast.makeText(getApplicationContext(),
  51.                 "你点击了第"+(id+1)+"个单位",
  52.                 Toast.LENGTH_SHORT).show();
  53.     }
  54.         
  55. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP