一、代码
1.xml(1)main.xml1 25 8 1211
(2)user.xml
1 27 10 13
2.java
(1)Activity01.java1 package mars.listview; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import android.app.ListActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.ListView;10 import android.widget.SimpleAdapter;11 12 public class Activity01 extends ListActivity {13 /** Called when the activity is first created. */14 @Override15 public void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.main);18 ArrayList> list = new ArrayList >();19 HashMap map1 = new HashMap ();20 HashMap map2 = new HashMap ();21 HashMap map3 = new HashMap ();22 map1.put("user_name", "zhangsan");23 map1.put("user_ip", "192.168.0.1");24 map2.put("user_name", "zhangsan");25 map2.put("user_ip", "192.168.0.2");26 map3.put("user_name", "wangwu");27 map3.put("user_ip", "192.168.0.3");28 list.add(map1);29 list.add(map2);30 list.add(map3);31 MyAdapter listAdapter = new MyAdapter(this, list,32 R.layout.user, new String[] { "user_name", "user_ip" },33 new int[] { R.id.user_name,R.id.user_ip});34 setListAdapter(listAdapter);35 }36 37 @Override38 protected void onListItemClick(ListView l, View v, int position, long id) {39 // TODO Auto-generated method stub40 super.onListItemClick(l, v, position, id);41 System.out.println("id----------------" + id);42 System.out.println("position----------" + position);43 }44 45 }
(2)MyAdapter.java
1 package mars.listview; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import android.R.color; 7 import android.content.Context; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.SimpleAdapter;12 13 public class MyAdapter extends SimpleAdapter {14 private LayoutInflater inflater = null;15 private List