標(biāo)注:本文為個人整理,僅做自己學(xué)習(xí)參考使用,請勿轉(zhuǎn)載和轉(zhuǎn)發(fā)
2018-06-15: 初稿。參考博主coder-pig
0. 引言
- 所有的UI控件都是和Adapter打交道的,所以了解和使用Adapter是很重要的,ADapter是用來幫助填充數(shù)據(jù)的中間的橋梁,簡單的電說,就是將和中數(shù)據(jù)以合適的形式顯示到view上,用戶就可以看到啦!
1. MVC模式的理解
- 首先要了解MVC模式,而什么叫做MVC模式呢,舉個栗子??
- 大型的商業(yè)程序通常由多人一同開發(fā)完成,比如有人負(fù)責(zé)操作接口的規(guī)劃與設(shè)計(jì)、有人負(fù)責(zé)程序的代碼的編寫。
-
需要對對程序項(xiàng)目的分工,就必須在程序的結(jié)構(gòu)上做合適的安排。如果接口設(shè)計(jì)與修改都合計(jì)到程序的代碼的話,那么兩者的分工就會造成執(zhí)行上的困難,所以需要良好的架構(gòu)師將整個程序項(xiàng)目劃分為如圖的三個部分
image - 對上圖的解析
- Model:通??梢岳斫鉃閿?shù)據(jù),負(fù)責(zé)執(zhí)行程序的可信運(yùn)算與判斷邏輯,通過view獲得用戶輸入的數(shù)據(jù),然后根據(jù)從數(shù)據(jù)庫查詢相關(guān)的信息,最后進(jìn)行運(yùn)算和判斷,再架構(gòu)得到的結(jié)果交給view
- view: 用戶的操作接口,說白了就是GUI,應(yīng)該使用哪種接口組件,組件見的排列位置與順序都需要設(shè)計(jì)
- Controller: 控制器,作為model與view之間的樞紐,負(fù)責(zé)控制程序的執(zhí)行流程以及對象之間的一個互動
- Adapter就是這個中間這個Controller的部分:Model數(shù)據(jù)->Controller以什么方式顯示->View用戶界面,這個就是簡單的MVC組件的理解。
2. Adapter概念的解析
- 官方文檔:Adapter
首先我們來看看他的繼承結(jié)構(gòu)圖:
- 上面就是Adapter以及繼承結(jié)構(gòu)圖了,接著我們介紹一下實(shí)際開發(fā)中還用到的幾個Adapter吧!
- BaseAdapter: 抽象類,實(shí)際開發(fā)中我們會繼承這個類,并且重寫相關(guān)方法,用的最多的一個Adapter
- ArrayAdapter: 支持范型操作,最簡單的Adapter,只能展現(xiàn)一行文字
- SimpleAdapter: 同樣具有良好的拓展性的Adapter,可以自定義多種效果
- SimpleCursorAdapter: 用于顯示簡單文本類型的ListView,一般在數(shù)據(jù)庫那里會用到,不過有點(diǎn)過時了
- 一般還是使用BaseAdapter,其他的實(shí)際開發(fā)并不多
3. 代碼示例
- Adpater一般都是結(jié)合ListView和GradView等控件,這里的控件是ListView
3.1 ArrayAdapter栗子
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//要顯示的數(shù)據(jù)
String[] strs = {"基神","B神","翔神","曹神","J神"};
//創(chuàng)建ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_expandable_list_item_1,strs);
//獲取ListView對象,通過調(diào)用setAdapter方法為ListView設(shè)置Adapter設(shè)置適配器
ListView list_test = (ListView) findViewById(R.id.list_test);
list_test.setAdapter(adapter);
}
}
一些相關(guān)的東西:
1.除了通過數(shù)組外,我們還可以寫到一個數(shù)組資源文件中:
比如:在res\valuse下創(chuàng)建一個數(shù)組資源的xml文件:arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myarray">
<item>語文</item>
<item>數(shù)學(xué)</item>
<item>英語</item>
</string-array>
</resources>
接著布局的listview屬性設(shè)置下這個列表項(xiàng):
<ListView
android:id="@id/list_test"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:entries="@array/myarray"/>
就可以了~
當(dāng)然我們也可以在Java代碼中這樣寫:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.myarray, android.R.layout.simple_list_item_multiple_choice );
同樣也是可以的!
其中android.R.layout.simple_list_item_multiple_choice 這個應(yīng)該是展示的形式,這種展示方式,后面還帶有一個小框框
- 因?yàn)锳rrayAdapter支持泛型,就可以添加集合啦
List<String> data = new ArrayList<String>();
data.add("基神");
data.add("B神");
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_expandable_list_item_1,data);
- 我們看到了實(shí)例化的ArrayAdapter的第二個參數(shù):android:R.layout.simple_expandable_list_item_1其實(shí)這些都是系統(tǒng)給我們提供好的一些ListView的模版。
simple_list_item_1 : 單獨(dú)一行的文本框
simple_list_item_2 : 兩個文本框組成

simple_list_item_checked : 每項(xiàng)都是由一個已選中的列表項(xiàng)

simple_list_item_multiple_choice : 都帶有一個復(fù)選框

simple_list_item_single_choice : 都帶有一個單選鈕

3.2 SimpleAdapter使用示例
- SimpleAdapter:簡單的Adapter,看似簡單,功能強(qiáng)大,比較復(fù)雜的列表布局
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- 定義一個用于顯示頭像的ImageView -->
<ImageView
android:id="@+id/imgtou"
android:layout_width="64dp"
android:layout_height="64dp"
android:baselineAlignBottom="true"
android:paddingLeft="8dp" />
<!-- 定義一個豎直方向的LinearLayout,把QQ呢稱與說說的文本框設(shè)置出來 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" />
<TextView
android:id="@+id/says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#B4B4B9"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] names = new String[]{"B神", "基神", "曹神"};
private String[] says = new String[]{"無形被黑,最為致命", "大神好厲害~", "我將帶頭日狗~"};
private int[] imgIds = new int[]{R.mipmap.head_icon1, R.mipmap.head_icon2, R.mipmap.head_icon3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> showitem = new HashMap<String, Object>();
showitem.put("touxiang", imgIds[i]);
showitem.put("name", names[i]);
showitem.put("says", says[i]);
listitem.add(showitem);
}
//創(chuàng)建一個simpleAdapter
SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.list_item, new String[]{"touxiang", "name", "says"}, new int[]{R.id.imgtou, R.id.name, R.id.says});
ListView listView = (ListView) findViewById(R.id.list_test);
listView.setAdapter(myAdapter);
}
}
3)SimpleCursorAdapter使用示例:
雖然這東西過時了,不過對于不怎么會SQLite的初學(xué)者來說,用起來還是蠻方便的! 記得前面我們學(xué)ContentProivder寫過的讀取聯(lián)系人的例子么?之前是通過打印Log的 方式顯示出來,現(xiàn)在我們通過這個SimpleCursorAdapter把它顯示到ListView上!
代碼實(shí)現(xiàn):
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/list_name"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_weight="1"
android:gravity="center"
android:text="小豬"
android:textColor="#0000FF"
android:textSize="18sp" />
<TextView
android:id="@+id/list_phone"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_weight="1"
android:gravity="center"
android:text="13798989898"
android:textColor="#EA5C4D"
android:textSize="18sp" />
</LinearLayout>
接著activity_main布局和前面的一樣,就是簡單的ListView,然后是
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_test = (ListView) findViewById(R.id.list_test);
//讀取聯(lián)系人
Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
SimpleCursorAdapter spcAdapter = new SimpleCursorAdapter(this,R.layout.list_item,cursor,
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[]{R.id.list_name,R.id.list_phone});
list_test.setAdapter(spcAdapter);
}
}
最后AndroidManifest.xml里加個讀聯(lián)系人的權(quán)限就可以了!
<uses-permission android:name="android.permission.READ_CONTACTS"/>
這個SimpleCursorAdapter還有沒有要注意的地方?使用SimpleCursorAdapter的話,綁定的數(shù)據(jù)庫表中一定要有id這個字段, 或者as id;而且在綁定時取出的數(shù)據(jù)必須包含這個id項(xiàng),否則的話會報以下錯誤! java.lang.IllegalArgumentException: column 'id' does not exist**
android 6.0 之后的這個讀取聯(lián)系人的權(quán)限需要動態(tài)申請了



