ListView 的使用

ListView控件使用起来比较特殊,不想其他Activity是继承Activivt的,ListView是继承了ListAtivity,并且使用适配器时,需要单独定义适配器中显示的样式。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="vertical" >
 5 
 6     <LinearLayout
 7         android:id="@+id/listviewlayout"
 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     </LinearLayout>
19 
20 </LinearLayout>
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="horizontal"
 5     android:paddingBottom="1dip"
 6     android:paddingLeft="10dip"
 7     android:paddingRight="10dip"
 8     android:paddingTop="1dip" >
 9 
10     <TextView
11         android:id="@+id/username"
12         android:layout_width="100dip"
13         android:layout_height="33dip"
14         android:singleLine="true"
15         android:textSize="10pt" />
16 
17     <TextView
18         android:id="@+id/userip"
19         android:layout_width="fill_parent"
20         android:layout_height="fill_parent"
21         android:gravity="right"
22         android:textSize="10pt" />
23 
24 </LinearLayout>
 1 package com.example.helloworld;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 
 6 import android.app.Activity;
 7 import android.app.ListActivity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.ListView;
11 import android.widget.SimpleAdapter;
12 
13 public class TenLessonDemo2 extends ListActivity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         // TODO Auto-generated method stub
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.tentwo);
20         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
21         HashMap<String, String> map1 = new HashMap<String, String>();
22         HashMap<String, String> map2 = new HashMap<String, String>();
23         HashMap<String, String> map3 = new HashMap<String, String>();
24         map1.put("username", "zhaogan1");
25         map1.put("userip", "zhaogan11");
26 
27         map2.put("username", "zhaogan2");
28         map2.put("userip", "zhaogan22");
29 
30         map3.put("username", "zhaogan3");
31         map3.put("userip", "zhaogan33");
32 
33         list.add(map1);
34         list.add(map2);
35         list.add(map3);
36         // android提供的最简单的适配器,适配器里面存放的键值应与上面的map中的键值一致
37         // 适配器里面的格式需要重新定义
38         SimpleAdapter listAdepter = new SimpleAdapter(this, list,
39                 R.layout.tenthree, new String[] { "username", "userip" },
40                 new int[] { R.id.username, R.id.userip });
41         // 上面的int 数组指的是在列表上面显示的问题标题
42         setListAdapter(listAdepter);
43     }
44 
45     @Override
46     protected void onListItemClick(ListView l, View v, int position, long id) {
47         // TODO Auto-generated method stub
48         super.onListItemClick(l, v, position, id);
49         System.out.println("id----------" + id);
50         System.out.println("position----------" + position);
51     }
52 
53 }
--------------------------------------------------------------------------------------------------------------------------------------------
顺势而为
原文地址:https://www.cnblogs.com/zhuzhenyu/p/2616116.html