- 论坛徽章:
- 0
|
首先是布局文件
1.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"
- >
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
-
- <ListView
- android:id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:drawSelectorOnTop="false"
- android:scrollbars="vertical"
- />
- </LinearLayout>
- </LinearLayout>
然后是user.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dip"
- >
- <TextView
- android:id="@+id/textUsrName"
- android:layout_width="180dip"
- android:layout_height="30dip"
- android:text="@string/usr_name"
- android:textSize="10pt"
- android:singleLine="true"
- android:gravity="left"
-
- />
- <TextView
- android:id="@+id/textUsrAddr"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/app_addr"
- android:textSize="10pt"
- android:gravity="right"
-
- />
- </LinearLayout>
string.xml中用到的一些变量
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, activity01!</STRING>
- <string name="app_name">listview</string>
- <string name="usr_name">用户名</string>
- <string name="app_addr">IP地址</string>
- </resources>
2.再就是java代码
- package listview.listview;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- public class activity01 extends ListActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- ArrayList<HashMap<String,String>> list = new
- ArrayList<HashMap<String,String>>();
- //创建表
- HashMap<String,String> map1 = new HashMap<String,String>();
- HashMap<String,String> map2 = new HashMap<String,String>();
- HashMap<String,String> map3 = new HashMap<String,String>();
- //初始化表数据
- map1.put("textUsrName", "小白兔");
- map1.put("textUsrAddr", "79.3.5.6");
- map2.put("textUsrName", "小黑");
- map2.put("textUsrAddr", "39.3.5.5");
- map3.put("textUsrName", "小红兔子");
- map3.put("textUsrAddr", "16.3.7.6");
- //将表添加进list
- list.add(map1);
- list.add(map2);
- list.add(map3);
- //新建容器adapter
- SimpleAdapter listAdapter = new SimpleAdapter(this,list,R.layout.user,
- new String[]{"textUsrName","textUsrAddr"},new
- int []{R.id.textUsrName,R.id.textUsrAddr});
- setListAdapter(listAdapter);
-
-
- }
-
-
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- // TODO Auto-generated method stub
- super.onListItemClick(l, v, position, id);
- System.out.println("id--------------"+(id+1));
- System.out.println("position--------------"+position);
- Toast.makeText(getApplicationContext(),
- "你点击了第"+(id+1)+"个单位",
- Toast.LENGTH_SHORT).show();
- }
-
- }
|
|