0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Adapter基礎(chǔ)講解

jf_12114301 ? 來源:jf_12114301 ? 作者:jf_12114301 ? 2023-03-31 18:22 ? 次閱讀

這一節(jié)我們要講的UI控件都是跟Adapter(適配器)打交道的,了解并學(xué)會(huì)使用Adapter很重要, Adapter是用來幫助填充數(shù)據(jù)的中間橋梁,簡(jiǎn)單來說就是:將各種數(shù)據(jù)以合適的形式顯示到view上,提供 給用戶看!

1.MVC模式的簡(jiǎn)單理解

在開始學(xué)習(xí)Adapter之前我們要來了解下這個(gè)MVC模式概念。 舉個(gè)例子:大型的商業(yè)程序通常由多人一同開發(fā)完成,比如有人負(fù)責(zé)操作接口的規(guī)劃與設(shè)計(jì), 有人負(fù)責(zé)程序代碼的編寫,如果要能夠做到程序項(xiàng)目的分工就必須在程序的結(jié)構(gòu)上做合適的安排 。如果接口設(shè)計(jì)與修改都涉及到程序代碼的改變的話,那么兩者的分工就會(huì)造成執(zhí)行上的困難, 良好的程序架構(gòu)師將整個(gè)程序項(xiàng)目劃分為如圖的三個(gè)部分:

poYBAGQlkmeAVlMUAABJnbI_nSY933.png

Model:通??梢岳斫鉃閿?shù)據(jù),負(fù)責(zé)執(zhí)行程序的核心運(yùn)算與判斷邏輯,,通過view獲得用戶 輸入的數(shù)據(jù),然后根據(jù)從數(shù)據(jù)庫(kù)查詢相關(guān)的信息,最后進(jìn)行運(yùn)算和判斷,再將得到的結(jié)果交給view來顯示

view:用戶的操作接口,說白了就是GUI,應(yīng)該使用哪種接口組件,組件間的排列位置與順序都需要設(shè)計(jì)

Controller:控制器,作為model與view之間的樞紐,負(fù)責(zé)控制程序的執(zhí)行流程以及對(duì)象之間的一個(gè)互動(dòng)

Adapter則是中間的這個(gè)Controller的部分:Model(數(shù)據(jù)) --->Controller(以什么方式顯示到)--->View(用戶界面) 這就是簡(jiǎn)單MVC組件的簡(jiǎn)單理解!

2.Adapter概念解析

首先我們來看看他的繼承結(jié)構(gòu)圖:

pYYBAGQlkmiACdutAAB8ERKWqVU057.png

上面就是Adapter以及繼承結(jié)構(gòu)圖了,接著我們介紹一下實(shí)際開發(fā)中還用到的幾個(gè)Adapter吧!

BaseAdapter:抽象類,實(shí)際開發(fā)中我們會(huì)繼承這個(gè)類并且重寫相關(guān)方法,用得最多的一個(gè)Adapter!

ArrayAdapter:支持泛型操作,最簡(jiǎn)單的一個(gè)Adapter,只能展現(xiàn)一行文字~

SimpleAdapter:同樣具有良好擴(kuò)展性的一個(gè)Adapter,可以自定義多種效果!

SimpleCursorAdapter:用于顯示簡(jiǎn)單文本類型的listView,一般在數(shù)據(jù)庫(kù)那里會(huì)用到,不過有點(diǎn)過時(shí), 不推薦使用!

3.代碼示例:

好的,多說無(wú)益,寫代碼最實(shí)際,接下來我們來用寫幾個(gè)簡(jiǎn)單的Adapter實(shí)例, 幫助我們了解Adapter給我們帶來的便利,另外,因?yàn)锳dapter需要結(jié)合ListView, GridView等等控件講解,一些高級(jí)一點(diǎn)的用法我們都放在ListView那里講!

1)ArrayAdapter使用示例:

運(yùn)行效果圖:

poYBAGQlkmiAJRg4AAAbcn8BKYE235.png

代碼實(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 adapter = new ArrayAdapter
                (this,android.R.layout.simple_expandable_list_item_1,strs);
        //獲取ListView對(duì)象,通過調(diào)用setAdapter方法為L(zhǎng)istView設(shè)置Adapter設(shè)置適配器
        ListView list_test = (ListView) findViewById(R.id.list_test);
        list_test.setAdapter(adapter);
    }
}

一些相關(guān)的東西:

1.除了通過數(shù)組外,我們還可以寫到一個(gè)數(shù)組資源文件中:

比如:在resvaluse下創(chuàng)建一個(gè)數(shù)組資源的xml文件:arrays.xml

   
  
      
    語(yǔ)文  
    數(shù)學(xué)  
    英語(yǔ)  
          

接著布局的listview屬性設(shè)置下這個(gè)列表項(xiàng):


就可以了~

當(dāng)然我們也可以在Java代碼中這樣寫:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
        R.array.myarray,android.R.layout.simple_list_item_multiple_choice );

同樣也是可以的!

2.一開始也說了這個(gè)ArrayAdapter支持泛型,那么集合必不可少啦,比如,這樣寫:

List data = new ArrayList();
data.add("基神");
data.add("B神");
ArrayAdapter adapter = new ArrayAdapter
                (this,android.R.layout.simple_expandable_list_item_1,data);

就可以了~

3.我們看到了在實(shí)例化ArrayAdapter的第二個(gè)參數(shù)android.R.layout.simple_expandable_list_item_1其實(shí)這些是系統(tǒng)給我們提供好的一些ListView模板。

2)SimpleAdapter使用示例:

SimpleAdapter:簡(jiǎn)單的Adapter,看似簡(jiǎn)單,功能強(qiáng)大,下面我們來寫個(gè)稍微復(fù)雜一點(diǎn)的列表 布局吧!

運(yùn)行效果圖:

pYYBAGQms4OAcfWTAABS0sEnOg0705.png

代碼實(shí)現(xiàn):

先來編寫一個(gè)列表項(xiàng)目每一項(xiàng)的布局:

list_item.xml

 


     
    

     
    

        

        

    


接下來是MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private String[] names = new String[]{"B神", "基神", "曹神"};
    private String[] says = new String[]{"無(wú)形被黑,最為致命", "大神好厲害~", "我將帶頭***~"};
    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> listitem = new ArrayList>();
        for (int i = 0; i < names.length; i++) {
            Map showitem = new HashMap();
            showitem.put("touxiang", imgIds[i]);
            showitem.put("name", names[i]);
            showitem.put("says", says[i]);
            listitem.add(showitem);
        }

        //創(chuàng)建一個(gè)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使用示例:

雖然這東西過時(shí)了,不過對(duì)于不怎么會(huì)SQLite的初學(xué)者來說,用起來還是蠻方便的! 記得前面我們學(xué)ContentProivder寫過的讀取聯(lián)系人的例子么?之前是通過打印Log的 方式顯示出來,現(xiàn)在我們通過這個(gè)SimpleCursorAdapter把它顯示到ListView上!

實(shí)現(xiàn)效果圖:

poYBAGQlkmqAExOqAABzm9Adr2A970.png

代碼實(shí)現(xiàn)

先寫下listView每個(gè)item的布局:

list_item.xml:

 



    

    


接著activity_main布局和前面的一樣,就是簡(jiǎn)單的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里加個(gè)讀聯(lián)系人的權(quán)限就可以了!


一問一答:

:就這么簡(jiǎn)單?——答:是的,直接獲取到Cursor,然后綁定就好了,無(wú)需你自己再寫什么SQL語(yǔ)句!:你說這東西過時(shí)了,那拿什么來代替?——答:一般的做法是自己重寫B(tài)aseAdapter,獲取到數(shù)據(jù)集合后跟對(duì)應(yīng)的控件進(jìn)行綁定!:這個(gè)SimpleCursorAdapter還有沒有要注意的地方?——答:有,使用SimpleCursorAdapter的話,綁定的數(shù)據(jù)庫(kù)表中一定要有id這個(gè)字段, 或者asid;而且在綁定時(shí)取出的數(shù)據(jù)必須包含這個(gè)id項(xiàng),否則的話會(huì)報(bào)以下錯(cuò)誤!java.lang.IllegalArgumentException: column 'id' does not exist**

審核編輯黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 接口
    +關(guān)注

    關(guān)注

    33

    文章

    8257

    瀏覽量

    149954
  • Adapter
    +關(guān)注

    關(guān)注

    0

    文章

    16

    瀏覽量

    7646
  • VIEW
    +關(guān)注

    關(guān)注

    0

    文章

    7

    瀏覽量

    9930
  • MVC
    MVC
    +關(guān)注

    關(guān)注

    0

    文章

    73

    瀏覽量

    13823
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    tcpip_adapter_start_api 函數(shù)的功能是什么?

    #define TCPIP_ADAPTER_IPC_CALL(_if, _mac, _ip, _data, _fn) do { tcpip_adapter_api_msg_t msg
    發(fā)表于 06-26 07:08

    adapter模式是如何去定義的

    adapter模式定義adapter 將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口。使用場(chǎng)景,兩個(gè)類所做的事情相同或者相似,但是具有不同的接口時(shí)要使用它。target這是客戶所期望的接口,目標(biāo)可以
    發(fā)表于 01-03 06:54

    基于OpenHarmony的Adapter使用系列資料匯總

    的實(shí)體類帶來影響。BaseNodeAdapter 說明: 繼承自 BaseProviderMultiAdapter,這是一個(gè)類似節(jié)點(diǎn)樹功能的 Adapter,具有展開收起節(jié)點(diǎn)的功能??梢詫?shí)現(xiàn)更自由
    發(fā)表于 03-23 16:35

    不能同時(shí)使用lpuart_adapter和uart_adapter嗎?

    與 RS485 收發(fā)器一起使用。 過去,我已經(jīng)編寫了一個(gè)簡(jiǎn)單的代碼來使用 uart_adapter 通過帶有緩沖區(qū)機(jī)制的 UART 發(fā)送和獲取字節(jié)。然后在 MCUExpresso 上,在 SDK 組件上,我添加
    發(fā)表于 03-24 07:36

    PL2303 USB to Serial Adapter

    The PL2303 USB to Serial adapter is your smart and convenient accessory forconnecting RS-23
    發(fā)表于 10-15 17:58 ?111次下載

    TOOLSTICK DEBUG ADAPTER USER’S

    TOOLSTICK DEBUG ADAPTER USER’S GUIDE:To enable development, the ToolStick Base Adapter and daughter
    發(fā)表于 09-09 12:39 ?6次下載

    iPod USB Power Adapter

    iPod USB Power AdapterThe iPod USB Power Adapter is for use withiPod shuffle, and with all iPods with DockConnectors.
    發(fā)表于 12-10 15:06 ?53次下載

    Interface Test Adapter (ITA) H

    DescriptionThe HP9421A Interface Test Adapter (ITA) providesconnections to and from the unit under
    發(fā)表于 07-09 17:36 ?8次下載

    Design of a QR Adapter with Im

    by Step Design Procedure5. Performances of a 60 W Adapter FeaturingValley Lockout
    發(fā)表于 07-30 08:58 ?19次下載

    什么是Host bus adapter

    什么是Host bus adapter  英文縮寫: Host bus adapter 中文譯名: 主機(jī)總線適配器 分  類: 網(wǎng)絡(luò)與交換 解  釋:
    發(fā)表于 02-23 10:03 ?1252次閱讀

    MERCURY Wireless N Adapter_1021.

    MERCURY Wireless N Adapter_1021.4.928.2012_5.1_x86驅(qū)動(dòng)文件
    發(fā)表于 11-04 15:53 ?31次下載

    120W ADAPTER的研發(fā)

    本文分析現(xiàn)狀,介紹一款飛兆科技股份有限公司專為 PTVCPU 筆記本電腦應(yīng)用的 12OWAdapter開關(guān)電源,以期提高Adapter的整體性能。
    發(fā)表于 05-10 13:45 ?7次下載

    dfrobotUSB Serial Light Adapter下載器簡(jiǎn)介

    USB Serial Light Adapter下載器用來對(duì)Arduino FIO/pro/mini、lilypad Arduino、x-board等模塊進(jìn)行程序下載。
    的頭像 發(fā)表于 12-13 15:00 ?1251次閱讀
    dfrobotUSB Serial Light <b class='flag-5'>Adapter</b>下載器簡(jiǎn)介

    adapter模式-手機(jī)電源適配例子

    adapter模式定義adapter 將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口。使用場(chǎng)景,兩個(gè)類所做的事情相同或者相似,但是具有不同的接口時(shí)要使用它。target 這是客戶所期望的接口,目標(biāo)可以
    發(fā)表于 01-12 11:47 ?0次下載
    <b class='flag-5'>adapter</b>模式-手機(jī)電源適配例子

    GD-Link Adapter 用戶 指南

    GD-Link Adapter 用戶 指南
    發(fā)表于 02-23 18:55 ?3次下載
    GD-Link <b class='flag-5'>Adapter</b> 用戶 指南