389
技術社區[雲棲]
Android 適配器教程(三)
前兩講中,我們分別大體的了解了適配器是什麼,和它的一些基本知識。
並且分別講解了了ArrayAdapter和SimpleCursorAdapter這兩種常用的安卓原生適配器,這兩種適配器都是封裝好了的,他們雖然使用起來非常簡單,但是可擴展性較差。
現在我們繼續向下深入學習安卓的適配器,這一次我們將會一同學習SimpleAdapter
simpleAdapter的擴展性最好,可以定義各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(複選框)等等,當然也有它做不到的事情,這些我們後來再說。
tip:有些代碼繼承了ListActivity,ListActivity和普通的Activity沒有太大的差別,不同就是對顯示ListView做了許多優化,方麵顯示而已。我們還是繼承自普通的Activity。
下麵我們要寫的實例程序是實現一個帶有圖片的類表。當然會用上SimpleAdapter
程序開始!
(1)也還是先在activity_main.xml裏添加一個button,一會跳轉的時候使用。
(2)然後新建一個類SimpleAdapterDemo繼承自Activity作為我們第三個例子的Activity,@Override 我們的onCreate方法。
(3)新建一個xml文件simpleadapterdemo.xml作為我們的布局文件,其中也是包含一個文本域和一個ListView:
代碼如下:
simpleadapterdemo.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是simpleadapter的一個例子" >
</TextView>
<ListView
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
注意啦!這裏和前兩次可有區別啦:
(4)我們需要定義好一個用來顯示每一個列內容的xml
listitem1.xml 包含橫向的圖片與文字,這裏可擴展性就體現出來了哦!
我們完全可以定義每一列的內容了,比如我可以讓每一列都是圖片並在圖片下麵加文字。
代碼如下:
listitem1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<TextView android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="22px"
android:layout_margin="5px"/>
<TextView android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="15px"
android:layout_margin="5px"/>
</LinearLayout>
(5)還是再回到SimpleAdapterDemo,定義一個Listview暫且叫做lv,並使用剛才的ID,setContentView()為剛才的xml布局,然後為lv設置一個適配器:
使用simpleAdapter的數據用一般都是HashMap構成的List,list的每一節對應ListView的每一行。HashMap的每個鍵值數據映射到布局文件中對應id的組件上。因為係統沒有對應的布局文件可用,我們可以自己定義一個布局listitem1.xml。
下麵做適配,new一個SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this,data, resource,from, to);
參數依次是:this,HashMap的 title和 info,布局文件(listitem1.xml),至於from和to,有木有很熟悉,對我們上一篇剛剛使用過了:From是你查詢出的數據,to是單行布局中的單獨控件,一對一的關係,用起來非常方便。布局文件的各組件分別映射到HashMap的各元素上,完成適配。
下麵是具體代碼:
package com.example.adapterdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SimpleAdapterDemo extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simpleadapterdemo);
lv = (ListView) findViewById(R.id.simpleadapterdemolistview);
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.listitem1,
new String[] { "text1", "text2", "img" }, new int[] {
R.id.text1, R.id.text2, R.id.imgview1});
lv.setAdapter(adapter);
}
public 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("text1", "Image1");
map.put("text2", "info1");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image2");
map.put("text2", "info2");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image3");
map.put("text2", "info3");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image4");
map.put("text2", "info4");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image5");
map.put("text2", "info5");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image6");
map.put("text2", "info6");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image7");
map.put("text2", "info7");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("text1", "Image8");
map.put("text2", "info8");
map.put("img", R.drawable.ic_launcher);
list.add(map);
return list;
}
}
最後又是看結果的時候了:
效果圖:

這樣三種常用的安卓原生適配器就算講完了,隨著一步步的學習,相信大家對適配器的使用已經比較有心得了。大家有沒有發現我們所使用的適配器威力越來越大,當然實現過程也更加繁瑣一些。
還有一些安卓的原生適配器,我們就殺雞儆猴的學習了用的最多的三個原生適配器,其他的也就是小菜了,接下來我們就要自定義適配器了,自定義的適配器能適應更多的情況,功能更加強大,當然也需要我們更加深入的學習才能應用自如,請大家不要錯過哦!
源碼我會在最後一篇寫完的時候一並上傳,以內我也是邊寫博客邊寫代碼,這樣我覺得思路清晰一些。
我也是學生,水平有限,還請多多指教~
最後更新:2017-04-03 05:39:34