Android ListView控件使用方法
ListView控件顯示列表有兩種方式,直接使用數(shù)組資源或者使用ArrayAdapter類,下面一個簡單的工程實(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);//獲得界面上的列表視圖控件
//定義一個數(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);
}
}
主界面定義兩個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為啟動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ā)之橫向滾動/豎向滾動的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ù)的動態(tài)添加與刪除實(shí)例代碼
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
相關(guān)文章
詳解Android App中ViewPager使用PagerAdapter的方法
這篇文章主要介紹了詳解Android App中ViewPager使用PagerAdapter的方法,同時附帶了一個ViewPager的PagerAdapter不能更新數(shù)據(jù)的問題解決方法,需要的朋友可以參考下2016-03-03
Android超詳細(xì)講解組件LinearLayout的使用
LinearLayout又稱作線性布局,是一種非常常用的布局。正如它的名字所描述的一樣,這個布局會將它所包含的控件在線性方向上依次排列。既然是線性排列,肯定就不僅只有一個方向,這里一般只有兩個方向:水平方向和垂直方向2022-03-03
Android?自定義開源庫?EasyView實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android自定義開源庫EasyView實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉頂部圖片變大,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android XMPP通訊自定義Packet&Provider
這篇文章主要介紹了Android XMPP通訊自定義Packet&Provider的相關(guān)資料,需要的朋友可以參考下2016-08-08
建造者模式_動力節(jié)點(diǎn)Java學(xué)院整理
建造者實(shí)現(xiàn)抽象類的所有未實(shí)現(xiàn)的方法,具體來說一般是兩項(xiàng)任務(wù),組建產(chǎn)品;返回組建好的產(chǎn)品2017-08-08
Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能
這篇文章主要介紹了Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08

