Android ListView控件使用方法
ListView控件顯示列表有兩種方式,直接使用數(shù)組資源或者使用ArrayAdapter類,下面一個(gè)簡(jiǎn)單的工程實(shí)現(xiàn)了這兩種方法。
ArrayAdapterList類:
public class ArrayAdapterList extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//關(guān)聯(lián)布局文件 ListView list2 = (ListView)findViewById(R.id.list2);//獲得界面上的列表視圖控件 //定義一個(gè)數(shù)組 String[] arr ={"易建聯(lián)","姚明","林書豪"}; //將數(shù)組包裝ArrayAdapter ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this , android.R.layout.simple_list_item_1 , arr); //為ListView設(shè)置Adapter list2.setAdapter(arrayAdapter); } }
主界面定義兩個(gè)ListView:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 直接使用數(shù)組資源給出列表項(xiàng) --> <ListView android:layout_width="fill_parent" android:layout_height="300dp" android:divider="@drawable/blue" android:entries="@array/books" android:headerDividersEnabled="false" > </ListView> <!-- 使用ArrayAdapter提供列表項(xiàng)的ListView --> <ListView android:id="@+id/list2" android:layout_width="fill_parent" android:layout_height="215dp" android:divider="@drawable/green" > </ListView> </LinearLayout>
數(shù)組資源:
<resources> <string-array name="books"> <item>奧尼爾</item> <item>鄧肯</item> <item>羅賓遜</item> <item>加內(nèi)特</item> </string-array> </resources>
配置文件,設(shè)置ArrayAdapterList為啟動(dòng)activity:
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="org.niit.listview.ArrayAdapterList" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
運(yùn)行效果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android listview優(yōu)化幾種寫法詳細(xì)介紹
- android開發(fā)教程之listview使用方法
- android開發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- Android自定義Adapter的ListView的思路及代碼
- Android下拉刷新ListView——RTPullListView(demo)
- Android之帶group指示器的ExpandableListView(自寫)
- android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法
- Android ListView的item背景色設(shè)置和item點(diǎn)擊無響應(yīng)的解決方法
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
相關(guān)文章
Android編程開發(fā)之EditText實(shí)現(xiàn)輸入QQ表情圖像的方法
這篇文章主要介紹了Android編程開發(fā)之EditText實(shí)現(xiàn)輸入QQ表情圖像的方法,涉及Android多媒體文件及EditText的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12Android添加自定義下拉刷新布局阻尼滑動(dòng)懸停彈動(dòng)畫效果
這篇文章主要為大家介紹了Android添加自定義下拉刷新布局阻尼滑動(dòng)懸停彈動(dòng)畫效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android優(yōu)化提升應(yīng)用啟動(dòng)速度及Splash頁面的設(shè)計(jì)
這篇文章主要介紹了Android性能優(yōu)化的一些相關(guān)資料,文章圍繞提升應(yīng)用啟動(dòng)速度及Splash頁面的設(shè)計(jì)的內(nèi)容展開介紹,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-12-12詳解Android使用OKHttp3實(shí)現(xiàn)下載(斷點(diǎn)續(xù)傳、顯示進(jìn)度)
本篇文章主要介紹了詳解Android使用OKHttp3實(shí)現(xiàn)下載(斷點(diǎn)續(xù)傳、顯示進(jìn)度),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android應(yīng)用中炫酷的橫向和環(huán)形進(jìn)度條的實(shí)例分享
這篇文章主要介紹了Android應(yīng)用中炫酷的橫向和圓形進(jìn)度條的實(shí)例分享,文中利用了一些GitHub上的插件進(jìn)行改寫,也是一片很好的二次開發(fā)教學(xué),需要的朋友可以參考下2016-04-04Android App支付系列(一):微信支付接入詳細(xì)指南(附官方支付demo)
這篇文章主要介紹了Android App支付系列(一):微信支付接入詳細(xì)指南(附官方支付demo) ,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Android開發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法匯總
這篇文章主要介紹了Android開發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法,下面小編把每種方法通過實(shí)例逐一給大家介紹,需要的朋友可以參考下2016-09-09android使用service和activity獲取屏幕尺寸的方法
這篇文章主要介紹了android使用service和activity獲取屏幕尺寸的方法,實(shí)例分析了基于service和activity兩種方法獲取屏幕尺寸的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08