Android入門(mén)之Adapter的簡(jiǎn)單使用教程
Adapter介紹
開(kāi)始進(jìn)入高級(jí)一些的組件的講解了,后面章節(jié)會(huì)大量使用到ListView,在講ListView前就必須帶著這個(gè)Adapter的概念去做講解。
Adapter其實(shí)就是MVC的概念, 舉個(gè)例子:大型的商業(yè)程序通常由多人一同開(kāi)發(fā)完成,比如有人負(fù)責(zé)操作接口的規(guī)劃與設(shè)計(jì), 有人負(fù)責(zé)程序代碼的編寫(xiě)如果要能夠做到程序項(xiàng)目的分工就必須在程序的結(jié)構(gòu)上做適合的安排。
上面就是Adapter以及繼承結(jié)構(gòu)圖了,接著我們介紹一下實(shí)際開(kāi)發(fā)中還用到的幾個(gè)Adapter吧!
BaseAdapter:抽象類(lèi),實(shí)際開(kāi)發(fā)中我們會(huì)繼承這個(gè)類(lèi)并且重寫(xiě)相關(guān)方法,用得最多的一個(gè)Adapter;
ArrayAdapter:支持泛型操作,最簡(jiǎn)單的一個(gè)Adapter,只能展現(xiàn)一行文字;
SimpleAdapter:同樣具有良好擴(kuò)展性的一個(gè)Adapter,可以自定義多種效果;
SimpleCursorAdapter:用于顯示簡(jiǎn)單文本類(lèi)型的listView,一般在數(shù)據(jù)庫(kù)那里會(huì)用到,已經(jīng)過(guò)時(shí),我們教程里不會(huì)講解;
多說(shuō)無(wú)益,寫(xiě)代碼最實(shí)際,接下來(lái)我們來(lái)用寫(xiě)幾個(gè)簡(jiǎn)單的Adapter實(shí)例, 幫助我們了解Adapter給我們帶來(lái)的便利,另外,因?yàn)锳dapter需要結(jié)合ListView, GridView等等控件講解,一些高級(jí)一點(diǎn)的用法我們都放在ListView那里講!
因此,現(xiàn)在我們就來(lái)看一個(gè)最簡(jiǎn)單的ListView的使用吧。
課程目標(biāo)
我們今天要實(shí)現(xiàn)這個(gè)ListView,使用的就是Adapter。
有一種Adapter叫ArrayAdapter<String> ,它的用法如下:
package org.mk.android.demo.demobasicadapter; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { List<String> titleList=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //要顯示的數(shù)據(jù) titleList.add("雷神"); titleList.add("基神"); titleList.add("基神"); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_expandable_list_item_1,titleList); //獲取ListView對(duì)象,通過(guò)調(diào)用setAdapter方法為L(zhǎng)istView設(shè)置Adapter設(shè)置適配器 ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(adapter); } }
我們來(lái)看這個(gè)簡(jiǎn)單例子的UI端代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
自然,它就能實(shí)現(xiàn)上述這個(gè)運(yùn)行效果。
當(dāng)然,它只是實(shí)現(xiàn)了一個(gè)很簡(jiǎn)單的List View的應(yīng)用。我們要實(shí)現(xiàn)進(jìn)一步更復(fù)雜點(diǎn)的如下面這種效果我們就需要使用到SimpleAdapter這個(gè)類(lèi):
SimpleAdapter:看似簡(jiǎn)單,功能強(qiáng)大,很多實(shí)際場(chǎng)景中其實(shí)都會(huì)使用到SimpleAdapter。我們會(huì)在下篇中著重講述如何使用Simple Adapter來(lái)實(shí)現(xiàn)上述這樣的一個(gè)復(fù)雜布局。
到此這篇關(guān)于Android入門(mén)之Adapter的簡(jiǎn)單使用教程的文章就介紹到這了,更多相關(guān)Android Adapter使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)自動(dòng)匹配關(guān)鍵字并且標(biāo)紅功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)匹配關(guān)鍵字并且標(biāo)紅功能,單關(guān)鍵字和多關(guān)鍵字進(jìn)行匹配,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android BottomSheet效果的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實(shí)現(xiàn)方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android 解決使用SearchView時(shí)軟鍵盤(pán)不支持actionSearch的問(wèn)題
本文主要介紹使用SearchView時(shí)軟鍵盤(pán)不支持actionSearch,這里提供了解決方案,希望能幫助開(kāi)發(fā)Android應(yīng)用的同學(xué)2016-07-07Android實(shí)現(xiàn)左右滑動(dòng)切換圖片
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左右滑動(dòng)切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android線(xiàn)程中設(shè)置控件的值提示報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Android線(xiàn)程中設(shè)置控件的值提示報(bào)錯(cuò)的解決方法,實(shí)例分析了textview報(bào)錯(cuò)的原因以及Handler設(shè)置來(lái)解決錯(cuò)誤的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06用Android Location獲取當(dāng)前地理位置的方法
本篇文章小編為大家介紹,用Android Location獲取當(dāng)前地理位置的方法。需要的朋友參考下2013-04-04Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
這篇文章主要介紹了Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能,結(jié)合實(shí)例形式分析了Android DatePicker和TimePicker組件的功能、常用函數(shù)、布局及日期時(shí)間選擇相關(guān)操作技巧,需要的朋友可以參考下2019-03-03