ANDROID_MARS学习笔记_S01原始版_006_ListView

一、代码
1.xml
(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" android:layout_width="fill_parent"
 4     android:layout_height="fill_parent">
 5     <LinearLayout android:id="@+id/listLinearLayout"
 6         android:layout_width="fill_parent" android:layout_height="wrap_content"
 7         android:orientation="vertical">
 8         <ListView android:id="@id/android:list" android:layout_width="fill_parent"
 9             android:layout_height="wrap_content" android:drawSelectorOnTop="true"
10             android:scrollbars="vertical" />
11     </LinearLayout>
12 </LinearLayout>

 

(2)user.xml

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent" android:layout_height="fill_parent"
 4     android:orientation="horizontal" android:paddingLeft="10dip"
 5     android:paddingRight="10dip" android:paddingTop="1dip"
 6     android:paddingBottom="1dip">
 7     <TextView android:id="@+id/user_name" android:layout_width="180dip"
 8         android:layout_height="30dip" android:textSize="5pt"
 9         android:singleLine="true" />
10     <TextView android:id="@+id/user_ip" android:layout_width="fill_parent"
11         android:layout_height="fill_parent" android:gravity="right"
12         android:textSize="5pt" />
13 </LinearLayout>

 

 

 

2.java
(1)Activity01.java

 1 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     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
19         HashMap<String, String> map1 = new HashMap<String, String>();
20         HashMap<String, String> map2 = new HashMap<String, String>();
21         HashMap<String, String> map3 = new HashMap<String, String>();
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     @Override
38     protected void onListItemClick(ListView l, View v, int position, long id) {
39         // TODO Auto-generated method stub
40         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<Map<String,Object>> styles = null;
16     public List<Map<String, Object>> getStyles() {
17         return styles;
18     }
19 
20     public void setStyles(List<Map<String, Object>> styles) {
21         this.styles = styles;
22     }
23 
24     @Override
25     public View getView(int position, View convertView, ViewGroup parent) {
26         // TODO Auto-generated method stub
27         View result = super.getView(position, convertView, parent);
28         System.out.println("positon---->" + position);
29         if(result != null){
30             inflater.inflate(R.layout.user1, null);
31         }
32         return result;
33     }
34 
35     public MyAdapter(Context context, List<? extends Map<String, ?>> data,
36             int resource, String[] from, int[] to) {
37         super(context, data, resource, from, to);
38         inflater = LayoutInflater.from(context);
39     }
40 
41 }

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S01原始版_006_ListView)