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

来源:互联网 发布:淘宝网一件代发赚钱吗 编辑:程序博客网 时间:2024/06/06 00:25
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. import java.util.ArrayList;
  3. import java.util.HashMap;

  4. import android.app.ListActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ListView;
  8. import android.widget.SimpleAdapter;
  9. import android.widget.Toast;

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


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. import android.app.ListActivity;
  7. import android.os.Bundle;
  8. import android.widget.SimpleAdapter;
  9. public class ListViewone extends ListActivity {

  10.         @Override
  11.         public void onCreate(Bundle savedInstanceState) {
  12.                 super.onCreate(savedInstanceState);

  13.                 SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.info,
  14.                                 new String[]{"name","phone","img"},
  15.                                 new int[]{R.id.name,R.id.phone,R.id.img});
  16.                 setListAdapter(adapter);
  17.         }

  18.         private List<Map<String, Object>> getData() {
  19.                 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

  20.                 Map<String, Object> map = new HashMap<String, Object>();
  21.                 map.put("name", "凝墨");
  22.                 map.put("phone", "13699782346");
  23.                 map.put("img", R.drawable.pic1);
  24.                 list.add(map);

  25.                 map = new HashMap<String, Object>();
  26.                 map.put("name", "小棕");
  27.                 map.put("phone", "15899034671");
  28.                 map.put("img", R.drawable.pic2);
  29.                 list.add(map);

  30.                 map = new HashMap<String, Object>();
  31.                 map.put("name", "花郎");
  32.                 map.put("phone", "18677656526");
  33.                 map.put("img", R.drawable.pic3);
  34.                 list.add(map);
  35.                 
  36.                 return list;
  37.         }
  38. }
复制代码


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.                 <TextView android:id="@+id/name" 
  13.                         android:layout_width="wrap_content"
  14.                         android:layout_height="wrap_content" 
  15.                         android:textColor="#FFFFFFFF"
  16.                         android:textSize="22px" />
  17.                 <TextView android:id="@+id/phone" 
  18.                         android:layout_width="wrap_content"
  19.                         android:layout_height="wrap_content" 
  20.                         android:textColor="#FFFFFFFF"
  21.                         android:textSize="13px" />
  22.         </LinearLayout>
  23. </LinearLayout>
复制代码


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

 
0 0
原创粉丝点击