Chinaunix首页 | 论坛 | 博客
  • 博客访问: 541898
  • 博文数量: 83
  • 博客积分: 4136
  • 博客等级: 中校
  • 技术积分: 886
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 21:14
文章分类

全部博文(83)

文章存档

2013年(1)

2011年(25)

2010年(57)

分类: 嵌入式

2011-08-15 09:05:33

首先是布局文件
1.main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.         <LinearLayout xmlns: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=""
  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!>
  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. }
阅读(7889) | 评论(0) | 转发(1) |
0

上一篇:关于内部类

下一篇:tomcat配置

给主人留下些什么吧!~~