Android之Adapter用法详解(2)

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

案例一

simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_margin
="5dp"
/>
<TextView
android:id="@+id/title"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textColor
="#ffffff"
android:textSize
="20sp"
/>
</LinearLayout>

public class SimpleAdapterActivity extends ListActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        
         SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[] { "title",  "img" }, new int[] { R.id.title, R.id.img });
         setListAdapter(adapter);
     }
    
     private List<Map<String, Object>> getData() {
         //map.put(参数名字,参数值)
         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("title", "摩托罗拉");
         map.put("img", R.drawable.icon);
         list.add(map);
        
         map = new HashMap<String, Object>();
         map.put("title", "诺基亚");
         map.put("img", R.drawable.icon);
         list.add(map);
        
         map = new HashMap<String, Object>();
         map.put("title", "三星");
         map.put("img", R.drawable.icon);
         list.add(map);
         return list;
         } 
    
}

案例二
  下面的程序是实现一个带有图片的类表。首先需要定义好一个用来显示每一个列内容的xml,vlist.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height
="fill_parent">  
        <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/>
        <LinearLayout android:orientation="vertical"  android:layout_width="wrap_content"  android:layout_height="wrap_content">
            <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor
="#FFFFFFFF" android:textSize="22px" />
            <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor
="#FFFFFFFF" android:textSize="13px" />
        </LinearLayout>
     </LinearLayout>

 

public class MyListView3 extends ListActivity {
        // private List<String> data = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
                    new String[]{"title","info","img"},
                    new int[]{R.id.title,R.id.info,R.id.img});
            setListAdapter(adapter);
        }
    
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }
    }

 

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

运行效果如下图:

Android之Adapter用法详解

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ppfxz.html