新聞中心
今天為大家?guī)淼氖怯嘘P(guān)Android ListActivity的相關(guān)內(nèi)容的介紹。我們可以從這篇文章中介紹的內(nèi)容詳細(xì)的對這一方面的知識進(jìn)行一個全面的認(rèn)識。首先看看android.app包里的幾個類。首先是這個在平臺自的例子中被廣泛使用的Android ListActivity。這個類其實(shí)就是一個含有一個ListView組件的Activity類。也就是說,如果我們直接在一個普通的Activity中自己加一個ListView也是完全可以取代這個Android ListActivity的,只是它更方便而已,方便到什么程度呢?來做個例子瞧瞧。

十載的朝陽網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整朝陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“朝陽網(wǎng)站設(shè)計(jì)”,“朝陽網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
- public class HelloTwoB extends ListActivity
- ...{
- public void onCreate(Bundle icicle) ...{
- super.onCreate(icicle);
- setTheme(android.R.style.Theme_Dark);
- setContentView(R.layout.mainb);
- List< String> items = fillArray();
- ArrayAdapter< String> adapter = new ArrayAdapter< String>
(this,R.layout.list_row,items);- this.setListAdapter(adapter);
- }
- private List< String> fillArray()
- ...{
- List< String> items = new ArrayList< String>();
- items.add("日曜日");
- items.add("月曜日");
- items.add("火曜日");
- items.add("水曜日");
- items.add("木曜日");
- items.add("金曜日");
- items.add("土曜日");
- return items;
- }
- @Override
- protected void onListItemClick(ListView l,
View v, int position, long id)- ...{
- TextView txt = (TextView)this.findViewById(R.id.text);
- txt.setText("あすは "+l.getSelectedItem().toString()+"です。");
- }
- }
的確可以簡單到只需準(zhǔn)備一個List對象并借助Adapter就可以構(gòu)造出一個列表。重載onListItemClick方法可以響應(yīng)選擇事件,利用***個參數(shù)可以訪問到這個ListView實(shí)例以得到選中的條目信息。這里有一點(diǎn)要說明的,就是如果更簡單的話,其實(shí)連那個setContentView都可以不要了,Android也會自動幫我們構(gòu)造出一個全屏的列表。但是本例中我們需要一個TextView來顯示選中的條目,所以我們需要一個layout.mainb描述一下這個列表窗口。
- < ?xml version="1.0" encoding="utf-8"?>
- < LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- < TextView id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- />
- < ListView id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"
- />
- < /LinearLayout>
在Android ListActivity操作中需要注意的是那個ListView的ID,是系統(tǒng)自定義的android:list,不是我們隨便取的,否則系統(tǒng)會說找不到它想要的listview了。然后,在這個listview之外,我們又增加了一個TextView,用來顯示選中的條目。
再來說說這里用到的ArrayAdapter,它的構(gòu)造函數(shù)中第二個參數(shù)是一個資源ID,ArrayAdapter的API文檔中說是要求用一個包含 TextView的layout文件,平臺用它來顯示每個選擇條目的樣式,這里的取值是R.layout.list_row,所以,我們還有一個list_row.xml文件來描述這個布局,相當(dāng)簡單。
- < ?xml version="1.0" encoding="utf-8"?>
- < LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- < TextView id="@+id/item"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- < TextView id="@+id/item2"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- < /LinearLayout>
從ArrayAdapter上溯到BaseAdapter,發(fā)現(xiàn)還有幾個同源的Adapter也應(yīng)該可以使用,象SimpleAdapter和CursorAdapter,還是做個例子來實(shí)驗(yàn)一下吧。
Android ListActivity的相關(guān)內(nèi)容就為大家介紹到這里。
當(dāng)前文章:AndroidListActivity應(yīng)用技巧全解
分享網(wǎng)址:http://www.dlmjj.cn/article/dphspji.html


咨詢
建站咨詢
