package com.example.listview_4;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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("userName", "张三");
map1.put("userAddr", "合肥市");
map2.put("userName", "李四");
map2.put("userAddr", "肥西县");
map3.put("userName", "王五");
map3.put("userAddr", "肥东县");
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[] { "userName", "userAddr" },
new int[] { R.id.textUsrName, R.id.textUsrAddr });
setListAdapter(listAdapter);
}// onCreate
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO 自动生成的方法存根
super.onListItemClick(l, v, position, id);
///id和position都是从0开始计数
System.out.println("id--------------" + (id + 1));
System.out.println("position--------------" + position);
Toast.makeText(getApplicationContext(), "你点击了第" + (id + 1) + "个单位",
Toast.LENGTH_SHORT).show();
}
}
------------------
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<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/user_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/user_addr"
android:textSize="10pt"
android:gravity="right"
/>
</LinearLayout>
------------
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ListView_4</string>
<string name="hello_world">Hello world!</string>
<string name="user_name">用户名</string>
<string name="user_addr">ip地址</string>
</resources>