好记性不如烂笔杆android学习笔记<五> ListView用法

15,//ListView
//main.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"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6     <LinearLayout 
 7         android:id="@+id/listLinearLayout"
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:orientation="vertical"
11         >
12         <ListView 
13             android:id="@+id/android:list"
14             android:layout_width="fill_parent"
15             android:layout_height="wrap_content"
16             android:drawSelectorOnTop="false"
17             android:scrollbars="vertical"
18             />
19     </LinearLayout>
20 </LinearLayout>

//适配器中调用的item文件的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"
 4     android:layout_height="fill_parent"
 5     android:orientation="horizontal"
 6     android:paddingLeft="10dip"
 7     android:paddingRight="10dip"
 8     android:paddingTop="10dip"
 9     android:paddingBottom="1dip" >
10     <TextView 
11         android:id="@+id/user_name"
12         android:layout_width="180dip"
13         android:layout_height="30dip"
14         android:textSize="10pt"
15         android:singleLine="true"        
16         />
17     <TextView 
18         android:id="@+id/user_ip"
19         android:layout_width="fill_parent"
20         android:layout_height="fill_parent"
21         android:textSize="10pt"
22         android:gravity="right"      
23         />
24 </LinearLayout>

//Java文件,使用ListView时,Activity时需要继承ListActivity

OnCreate方法中 

 1 ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
 2 HashMap<String,String> map1 = new HashMap<String,String>();
 3 map1.put("user_name", "zhangsan");
 4 map1.put("user_ip", "192.168.0.1");
 5 list.add(map1);
 6 //适配器 ,这里调用user.xml文件
 7 SimpleAdapter listAdapter = new SimpleAdapter(this,
 8         list,R.layout.user,
 9         new String[]{"user_name","user_ip"},
10         new int[]{R.id.user_name,R.id.user_ip});
11 setListAdapter(listAdapter); 
12 }
13 //点击listView的item时,使用onListItemClick方法
14 @Override
15 protected void onListItemClick(ListView l, View v, int position, long id) {
16     // TODO Auto-generated method stub
17     super.onListItemClick(l, v, position, id);
18     System.out.println("id-------------"+ id);
19     System.out.println("position-------------"+ position);
20 } 

<2>//ListView,例子2

//Java文件,使用ListView

 1 ArrayList<HashMap<String,String>> mlist = new ArrayList<HashMap<String,String>>();
 2 HashMap<String,String> map = new HashMap<String,String>();
 3 map.put("nav_adapter_img", "");
 4 map.put("nav_adapter_name", "audio1234.mp3");
 5 map.put("nav_adapter_size", "300k");
 6 map.put("nav_adapter_Right_img", "");
 7 myListView = (ListView) findViewById(R.id.mylistview);
 8 mlist.add(map);
 9 myListView.setAdapter(new MyListViewAdapter(this,mlist));        
10 //click the ListView
11 myListView.setOnItemClickListener(new OnItemClickListener() {
12 
13         @Override
14         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
15                 long arg3) {
16             Intent intent = new Intent();
17             intent.setClass(TestActivity.this, summaryActivity.class);
18             intent.putExtra("Click", "There is the ListView onItemClick method------------------------->");
19             startActivity(intent);
20         }
21     
22     });
23 //set up a list view
24 myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
25 myListView.setOnItemLongClickListener(new OnCreateContextMenuListener());
26 myListView.setFastScrollEnabled(true);
27 registerForContextMenu(myListView);
28 //创建MyListViewAdapter适配器类,继承于BaseAdapter
29 static class MyListViewAdapter extends BaseAdapter{
30 ArrayList<HashMap<String,String>> mlist;
31 LayoutInflater mLayoutInflater;
32 Context mContext;
33     public MyListViewAdapter(Context mContext,ArrayList<HashMap<String, String>> mlist) {
34         super();
35         this.mlist = mlist;
36         mLayoutInflater = LayoutInflater.from(mContext);
37         this.mContext =mContext;
38     }
39     @Override
40     public int getCount() {
41         return mlist.size();
42     }
43     @Override
44     public Object getItem(int position) {
45         return mlist.get(position);
46     }
47     @Override
48     public long getItemId(int position) {
49         return position;
50     }
51     @Override
52     public View getView(int position, View convertView, ViewGroup parent) {
53         if(convertView == null)
54         {
55             convertView = mLayoutInflater.inflate(R.layout.items, null);
56         }
57         TextView tv = (TextView) convertView.findViewById(R.id.nav_adapter_name);
58         tv.setText(((HashMap<String,String>)getItem(position)).get("nav_adapter_name"));
59         tv = (TextView) convertView.findViewById(R.id.nav_adapter_size);
60         tv.setText(((HashMap<String,String>)getItem(position)).get("nav_adapter_size"));
61         //imageView test
62         ImageView img = (ImageView)convertView.findViewById(R.id.nav_adapter_Right_img);
63         img.setOnClickListener(new OnClickListener() {
64             @Override
65             public void onClick(View v) {
66                 Intent intent = new Intent();
67                 intent.setClass(mContext, summaryActivity.class);
68                 intent.putExtra("Click", "There is the ImageView onClick method----------------------->" +
69                         "test,test,test,test,test,test,test,test,test,test,test,test,test,test," +
70                         "test,test,test,test,test,test,test,test,test" );
71                 mContext.startActivity(intent);
72             }
73         });
74         return convertView;
75     }
76 }
原文地址:https://www.cnblogs.com/zjqlogs/p/2779225.html