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