Android ListView控件使用方法
ListView控件顯示列表有兩種方式,直接使用數(shù)組資源或者使用ArrayAdapter類,下面一個(gè)簡單的工程實(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)容,希望對大家的學(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 App中ViewPager使用PagerAdapter的方法
這篇文章主要介紹了詳解Android App中ViewPager使用PagerAdapter的方法,同時(shí)附帶了一個(gè)ViewPager的PagerAdapter不能更新數(shù)據(jù)的問題解決方法,需要的朋友可以參考下2016-03-03
Android超詳細(xì)講解組件LinearLayout的使用
LinearLayout又稱作線性布局,是一種非常常用的布局。正如它的名字所描述的一樣,這個(gè)布局會(huì)將它所包含的控件在線性方向上依次排列。既然是線性排列,肯定就不僅只有一個(gè)方向,這里一般只有兩個(gè)方向:水平方向和垂直方向2022-03-03
Android?自定義開源庫?EasyView實(shí)現(xiàn)詳解
Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
Android創(chuàng)建淡入淡出動(dòng)畫的詳解
Android XMPP通訊自定義Packet&Provider
建造者模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能

