玩转Android---UI篇---ListView之SampleAdapter(列表)---1

来源:互联网 发布:php实现导出excel 编辑:程序博客网 时间:2024/05/17 23:01

原址:http://hualang.iteye.com/category/143855



ListView是列表组件,这个ListView是我接触的目前所有Android UI控件中最为麻烦的控件,之所以麻烦就是因为它的各种的适配器Adapter特别麻烦,Adapter的组织结构图如下



 在ListView中,以内不同的Adapter不同,所以也会有不同的效果,其中比较常用的是SampleAdapter,SimpleCursorAdapter,ArrayAdapter,BaseAdapter等,

万事开头难,还是从最简单的SimpleAdapter说起,以后再一点点学习

 

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

 

先看看一个实例,是由SimpleAdapter与ListView绑定后的一个小例子。

ListViewone.java文件

 

Java代码  收藏代码
  1. package org.hualang.simpleadapter;  
  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. import android.widget.Toast;  
  12.   
  13. public class ListViewone extends ListActivity {  
  14.     /** Called when the activity is first created. */  
  15.     private Toast toast;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  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("name""凝墨");  
  25.         map1.put("phone""13699452790");  
  26.         map2.put("name""小棕");  
  27.         map2.put("phone""15827980910");  
  28.         map3.put("name""花郎");  
  29.         map3.put("phone""18678091166");  
  30.           
  31.         list.add(map1);  
  32.         list.add(map2);  
  33.         list.add(map3);  
  34.         SimpleAdapter listAdapter=new SimpleAdapter(this,  
  35.                 list,  
  36.                 R.layout.info,  
  37.                 new String[] {"name","phone"},  
  38.                 new int[] {R.id.name,R.id.phone});  
  39.         setListAdapter(listAdapter);  
  40.     }  
  41.     protected void onListItemClick(ListView l,View v,int position,long id)  
  42.     {  
  43.         super.onListItemClick(l,v,position,id);  
  44.         if(l.getItemIdAtPosition(position)==0)  
  45.         {  
  46.             toast.makeText(getApplicationContext(), "我是凝墨", Toast.LENGTH_SHORT).show();  
  47.         }else if(l.getItemIdAtPosition(position)==1)  
  48.         {  
  49.             toast.makeText(getApplicationContext(), "我是小棕", Toast.LENGTH_SHORT).show();  
  50.         }else if(l.getItemIdAtPosition(position)==2)  
  51.         {  
  52.             toast.makeText(getApplicationContext(), "我是花郎", Toast.LENGTH_SHORT).show();  
  53.         }  
  54.           
  55.     }  
  56.       
  57. }  

 main.xml文件

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/linearlayout"  
  11.         android:orientation="vertical"  
  12.     >  
  13.         <ListView  
  14.             android:id="@id/android:list"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:drawSelectorOnTop="false"  
  18.             android:scrollbars="vertical"  
  19.         />  
  20.     </LinearLayout>  
  21. </LinearLayout>  

 info.xml

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:paddingLeft="10dip"  
  7.   android:paddingRight="10dip"  
  8.   android:paddingTop="1dip"  
  9.   android:paddingBottom="1dip"  
  10.   >  
  11.   <TextView  
  12.     android:id="@+id/name"  
  13.     android:layout_width="180dip"  
  14.     android:layout_height="30dip"  
  15.     android:textSize="10pt"  
  16.     android:singleLine="true"  
  17.   />  
  18.   <TextView  
  19.     android:id="@+id/phone"  
  20.     android:layout_width="fill_parent"  
  21.     android:layout_height="fill_parent"  
  22.     android:gravity="right"  
  23.     android:textSize="10pt"  
  24.   />  
  25. </LinearLayout>  

 使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局info.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(info.xml)。布局文件的组件name,phone。布局文件的各组件分别映射到HashMap的各元素上,完成适配。

 

运行结果如下:



 当点击了第一行


 

 

实例2:显示一个带图片的ListView,使用适配器SampleAdapter

ListViewone.java

Java代码  收藏代码
  1. package org.hualang.simpleadapter;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import android.app.ListActivity;  
  8. import android.os.Bundle;  
  9. import android.widget.SimpleAdapter;  
  10. public class ListViewone extends ListActivity {  
  11.   
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.   
  16.         SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.info,  
  17.                 new String[]{"name","phone","img"},  
  18.                 new int[]{R.id.name,R.id.phone,R.id.img});  
  19.         setListAdapter(adapter);  
  20.     }  
  21.   
  22.     private List<Map<String, Object>> getData() {  
  23.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  24.   
  25.         Map<String, Object> map = new HashMap<String, Object>();  
  26.         map.put("name""凝墨");  
  27.         map.put("phone""13699782346");  
  28.         map.put("img", R.drawable.pic1);  
  29.         list.add(map);  
  30.   
  31.         map = new HashMap<String, Object>();  
  32.         map.put("name""小棕");  
  33.         map.put("phone""15899034671");  
  34.         map.put("img", R.drawable.pic2);  
  35.         list.add(map);  
  36.   
  37.         map = new HashMap<String, Object>();  
  38.         map.put("name""花郎");  
  39.         map.put("phone""18677656526");  
  40.         map.put("img", R.drawable.pic3);  
  41.         list.add(map);  
  42.           
  43.         return list;  
  44.     }  
  45. }  

 info.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <ImageView android:id="@+id/img"   
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"   
  8.         android:layout_margin="5px"/>  
  9.     <LinearLayout android:orientation="vertical"  
  10.         android:layout_width="wrap_content"   
  11.         android:layout_height="wrap_content">  
  12.   
  13.         <TextView android:id="@+id/name"   
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"   
  16.             android:textColor="#FFFFFFFF"  
  17.             android:textSize="22px" />  
  18.         <TextView android:id="@+id/phone"   
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"   
  21.             android:textColor="#FFFFFFFF"  
  22.             android:textSize="13px" />  
  23.     </LinearLayout>  
  24. </LinearLayout>  

 这里,就不做事件处理了,运行结果如下:



0 0
原创粉丝点击