ListView的Adapter使用 之 初學(xué)ArrayAdapter String
ListView是Android中經(jīng)常會使用的東西,綁定數(shù)據(jù)對于初學(xué)者來說,尤其是剛接觸編程的人來說,往往會覺得很難理解,我上大二的時候?qū)W的java,但是基本上相當(dāng)于沒有學(xué),什么都沒寫過,真正接觸編程就是開始上手學(xué)android,把這些記錄下來,自己可以回頭看下,也可以讓新手更好的理解。高手繞過....
Android中Adapter我是這么理解的,是數(shù)據(jù)和視圖之間的橋梁,數(shù)據(jù)在adapter中做處理,然后顯示到視圖上面。
Adapter有很多種,有ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter.
我在項目中用到過的就ArrayAdapter<T>, (數(shù)組也可以是對象數(shù)組),BaseAdapter(所有Adapter的基類),SimpleAdapter,CursorAdapter(數(shù)據(jù)來源是cursor),SimpleCursorAdapter,感覺有必要總結(jié)一下。
最基本的要數(shù)sdk官網(wǎng)上面給的這個代碼例子了http://developer.android.com/resources/tutorials/views/hello-listview.html。
我自己寫的一個例子,先上圖,再貼上代碼:
package com.cz.list.demo;
import android.app.Activity; import android.os.Bundle;
import android.widget.ArrayAdapter; import android.widget.ListView;
public class ArrayListDemo extends Activity {
private ListView listView; private String[] adapterData;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.array_list_layout);
/* 找到這個listView */
listView = (ListView) findViewById(R.id.array_list);
/* 我們要在listView上面沒條顯示的數(shù)據(jù),放到一個數(shù)組中 */ adapterData = new String[] { "Afghanistan", "Albania", "Algeria",
"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",
"Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
"Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island" };
/* 這個是數(shù)組string類型的數(shù)組 */
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( // ArrayListDemo.this, android.R.layout.simple_list_item_1,
// adapterData);
/* 設(shè)置ListView的Adapter */ listView.setAdapter(new ArrayAdapter<String>(ArrayListDemo.this,
android.R.layout.simple_list_item_1, adapterData)); }
}
代碼中寫的有注釋,我覺得要解釋的有幾點,都是很基礎(chǔ)的,高手就見笑了.
1. 適配器的作用是數(shù)據(jù)和視圖之間的橋梁
2. 這個小例子是要顯示一個數(shù)組,我們就用ArrayAdapter,數(shù)組適配器,數(shù)據(jù)的數(shù)據(jù)類型<>是String類型的,數(shù)據(jù)的數(shù)據(jù)類型還可以是其他的包括對象類型的
3. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
ArrayListDemo.this, android.R.layout.simple_list_item_1,
adapterData);
這段代碼是創(chuàng)建一個數(shù)組適配器的代碼,里面有三個參數(shù),第一個參數(shù)是上下文,就是當(dāng)前的Activity, 第二個參數(shù)是android sdk中自己內(nèi)置的一個布局,它里面只有一個TextView,這個參數(shù)是表明我們數(shù)組中每一條數(shù)據(jù)的布局是這個view,就是將每一條數(shù)據(jù)都顯示在這個view上面;第三個參數(shù)就是我們要顯示的數(shù)據(jù)。listView會根據(jù)這三個參數(shù),遍歷adapterData里面的每一條數(shù)據(jù),讀出一條,顯示到第二個參數(shù)對應(yīng)的布局中,這樣就形成了我們看到的listView. 不知道剛學(xué)的同學(xué)懂了沒有...
本文出自 “生如夏花” 博客
- Android自定義Adapter的ListView的思路及代碼
- android開發(fā)中ListView與Adapter使用要點介紹
- ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項的布局去綁定數(shù)據(jù)
- Adapter實現(xiàn)ListView帶多選框等狀態(tài)的自定義控件的注意事項
- Android listview與adapter詳解及實例代碼
- Android開發(fā)中ListView自定義adapter的封裝
- 揭秘在ListView等AdapterView上動態(tài)添加刪除項的陷阱
- ListView Adapter優(yōu)化 實例
- Android ListView適配器(Adapter)優(yōu)化方法詳解
- Android ListView自定義Adapter實現(xiàn)仿QQ界面
相關(guān)文章
Flutter質(zhì)感設(shè)計之進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計之進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Android?kotlin?跳轉(zhuǎn)手機熱點開關(guān)頁面和判斷熱點是否打開(親測可用)
跳轉(zhuǎn)手機熱點的頁面肯定是用intent,重點是action不知道是什么,網(wǎng)上最常見的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁面,本文介紹Android?kotlin?跳轉(zhuǎn)手機熱點開關(guān)頁面和判斷熱點是否打開,感興趣的朋友一起看看吧2023-08-08Android ndk獲取手機內(nèi)部存儲卡的根目錄方法
今天小編就為大家分享一篇Android ndk獲取手機內(nèi)部存儲卡的根目錄方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android中butterknife的使用與自動化查找組件插件詳解
這篇文章主要給大家介紹了關(guān)于Android中butterknife的使用與自動化查找組件插件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10android編程實現(xiàn)設(shè)置、打開wifi熱點共享供他人連接的方法
這篇文章主要介紹了android編程實現(xiàn)設(shè)置、打開wifi熱點共享供他人連接的方法,涉及Android創(chuàng)建WiFi及設(shè)置共享的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11android自定義popupwindow仿微信右上角彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了android自定義popupwindow仿微信右上角彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android開發(fā)之Gradle?進(jìn)階Tasks深入了解
這篇文章主要為大家介紹了Android開發(fā)之Gradle?進(jìn)階Tasks深入了解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08