android 开发ListView总结
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 1484
一、界面效果---------------------------------------------------------------------------------------------

一、界面效果

-------------------------------------------------------------------------------------------------------------------------------------------------------------

二、步骤

①数据准备:直接用一维或多维动态数组保存数据;构造对象通过toString方式展示数据;联系人数据库
②适配器选择:ArrayAdapter;SimpleAdapter;SimpleCursorAdapter
③ListView显示数据
④事件响应(设置监听器实现单击、滚动、单击长按响应等)

-------------------------------------------------------------------------------------------------------------------------------------------------------------

三、源码
1 总括

①总布局文件:listview_demo.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="wrap_content"   5     android:orientation="vertical" >   6    7     <TextView   8 android:layout_width="fill_parent"   9         android:layout_height="wrap_content"  10         android:gravity="center_horizontal"  11         android:paddingBottom="25dp"  12         android:paddingTop="15dp"  13         android:text="SkySeraph学习专题:ListView"  14         android:textColor="#FFFF00"  15         android:textSize="20dp" >  16     </TextView>  17       18     <LinearLayout  19 android:layout_width="fill_parent"  20         android:layout_height="wrap_content"          21         android:orientation="vertical"   22         android:layout_gravity="center">  23   24         <Button  25 android:id="@+id/listview_demo_btn01"  26             android:layout_width="fill_parent"  27             android:layout_height="wrap_content"  28             android:text="ListView+ArrayAdapter实现一条信息显示" >  29         </Button>  30   31         <Button  32 android:id="@+id/listview_demo_btn02"  33             android:layout_width="fill_parent"  34             android:layout_height="wrap_content"  35             android:text="ListView+ArrayAdapter实现自定义多条信息显示" >  36         </Button>  37   38         <Button  39 android:id="@+id/listview_demo_btn03"  40             android:layout_width="fill_parent"  41             android:layout_height="wrap_content"  42             android:text="ListView+SimpleCursorAdapter" >  43         </Button>  44   45         <Button  46 android:id="@+id/listview_demo_btn04"  47             android:layout_width="fill_parent"  48             android:layout_height="wrap_content"  49             android:text="ListView+SimpleAdapter" >  50         </Button>  51   52         <Button  53 android:id="@+id/listview_demo_btn05"  54             android:layout_width="fill_parent"  55             android:layout_height="wrap_content"  56             android:text="继承ListActivity" >  57         </Button>  58     </LinearLayout>  59   60 </LinearLayout>

②注意
需要在AndroidManifest文件中添加五个listview_demo的activity
并需要添加权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

-------------------------------------------------------------------------------------------------------------------------------------------------------------

2 测试1:ListView+ArrayAdapter实现一条信息显示
①效果

②布局文件:listview_demo01.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="wrap_content"   5     android:orientation="vertical" >   6    7     <ListView   8 android:id="@+id/listview_demo01_list"   9         android:layout_width="fill_parent"  10         android:layout_height="fill_parent" />  11   12 </LinearLayout>

③java源码:listview_demo01.java

   1 public class listview_demo01 extends Activity implements OnItemClickListener   2 {   3     // ////////////////////////////////////////////////////////////////////////////////////   4     ListView m_ListView;   5     // ////////////////////////////////////////////////////////////////////////////////////   6 //①数据来源:方式一   7     final String[] data =   8     { "first", "secend", "third", "fourth", "fifth" };   9     //①数据来源:方式二  10     private List<String> getData()  11     {  12         List<String> data = new ArrayList<String>();  13         data.add("first");  14         data.add("Second");  15         data.add("Third");  16         data.add("Fourth");  17         data.add("Fifth");  18         return data;  19     }  20     // ////////////////////////////////////////////////////////////////////////////////////  21     @Override  22     protected void onCreate(Bundle savedInstanceState)  23     {  24         // TODO Auto-generated method stub  25         super.onCreate(savedInstanceState);  26         setContentView(R.layout.listview_demo01);  27         findViews();  28     }  29       30     // ////////////////////////////////////////////////////////////////////////////////////  31     private void findViews()  32     {          33         m_ListView = (ListView) findViewById(R.id.listview_demo01_list);  34         //②建立适配器  35         /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  36                 android.R.layout.simple_list_item_1, data);*/          37         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  38                 android.R.layout.simple_list_item_1, getData());  39         /*android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字*/  40         //③显示  41         m_ListView.setAdapter(adapter);    //注册          42 //④事件  43         m_ListView.setOnItemClickListener(this);//设置监听器          44     }  45     // ////////////////////////////////////////////////////////////////////////////////////  46     @Override  47     public void onItemClick(AdapterView<?> parent, View view, int position, long id)  48     {  49         // TODO Auto-generated method stub  50         Log.i("TAG",  51                 data[position] + "postion=" + String.valueOf(position) + "row_id="  52                         + String.valueOf(id));  53     }  54     // ////////////////////////////////////////////////////////////////////////////////////  55 }

④说明:事件采用了单击事件并通过Log输出

-------------------------------------------------------------------------------------------------------------------------------------------------------------

3 测试2:ListView+ArrayAdapter实现自定义多条信息显示
①效果:

②布局文件:listview_demo02.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="wrap_content"   5     android:orientation="vertical" >   6    7     <ListView   8 android:id="@+id/listview_demo02_list"   9         android:layout_width="fill_parent"  10         android:layout_height="fill_parent" />  11   12 </LinearLayout>

③java源码:listview_demo02.java

   1 public class listview_demo02 extends Activity   2 {   3     // ////////////////////////////////////////////////////////////////////////////////////   4     Person[] data = new Person[]   5     { new Person("蔡志坤", 25, "ffczk86@gmail.com", "厦门市"), new Person("李杰华", 25, "aa@bb.com", "漳州市"),   6             new Person("张亮", 25, "cc@gmail.com", "厦门市"),   7             new Person("陈旭", 25, "ccadd@gmail.com", "厦门市"),   8             new Person("刘玄德", 25, "ffczk86@gmail.com", "福州市") };   9   10     // ////////////////////////////////////////////////////////////////////////////////////  11     @Override  12     protected void onCreate(Bundle savedInstanceState)  13     {  14         // TODO Auto-generated method stub  15         super.onCreate(savedInstanceState);  16         setContentView(R.layout.listview_demo02);  17         findViews();  18     }  19   20     private void findViews()  21     {  22         // ////////////////////////////////////////////////////////////////////////////////////  23         ListView listView = (ListView) findViewById(R.id.listview_demo02_list);  24         ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,  25                 android.R.layout.simple_list_item_1, data);// 建立适配器  26         listView.setAdapter(adapter);// 注册  27 // ////////////////////////////////////////////////////////////////////////////////////  28     }  29     // ////////////////////////////////////////////////////////////////////////////////////  30     public class Person  31     {  32         public String name;  33         public int age;  34         public String email;  35         public String address;  36   37         public Person(String name, int age, String email, String address)  38         {  39             super();  40             this.name = name;  41             this.age = age;  42             this.email = email;  43             this.address = address;  44         }  45         @Override  46         public String toString()  //关键  47         {  48             return "Person [name=" + name + ", age=" + age + ", email=" + email + ", address="  49                     + address + "]";  50         }  51     }  
        
    
联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2