溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android ApiDemo中如何理解ListActivity

發(fā)布時間:2021-11-26 11:28:14 來源:億速云 閱讀:127 作者:柒染 欄目:移動開發(fā)

本篇文章為大家展示了Android ApiDemo中如何理解ListActivity,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)建了ApiDemo工程后,我們就可以進行每個示例代碼的分析了。

首先是看ApiDemo的主Activity:com.example.android.apis.ApiDemos,這個主Activity為ListActivity的子類,主要用來列出ApiDemos中的200多個實例,實例采取分類層次顯示。

在ApiDemos的onCreate()中的代碼:

setListAdapter(new SimpleAdapter(this, getData(path),     android.R.layout.simple_list_item_1, new String[] { "title" },     new int[] { android.R.id.text1 }));

SimpleAdatper 作為數(shù)據(jù)源 getData(path) 與 UI ListActivity 之間的橋梁,它的構造函數(shù)如下:

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

我們知道ListActivity可以用來顯示一個列表,在使用SimpleAdapter時可以借用二維表來更好的理解。  SimpleAdapter的數(shù)據(jù)源data 類型為List<? extends Map<String, ?>> List  中每一項為一個Map對象,相當于二維表中一行,這一行可以有多列,每列可以有個名字,為Map<String,?> string  ,相當于表的列名:

Android ApiDemo中如何理解ListActivity

ApiDemos中每條記錄只顯示一列”title”。 android.R.layout.simple_list_item_1 為用來顯示每條記錄的Layout資源id, ListActivity允許使用自定義Layout ,這里使用了Android系統(tǒng)資源,simple_list_item_1由一個TextView構成,其id為text1。

new String[] { “title” } 為需要顯示的列表的數(shù)組,ApiDemos只顯示一列“title”,如果有多列:則可以為new String[] { “title”,”field1”,”field2”,”field3” }。

new  int[] { android.R.id.text1 }則指定使用 android.R.layout.simple_list_item_1 中  id 為text1的 TextView 來顯示 “title” 列。 如果有多列,Layout可以定義多個View (不一定都為TextView),然后為每列指定顯示的View的id。

再來看看getData(path)是如何定義的,protected List getData(String prefix) 返回一個列表。

protected List getData(String prefix) {    List<Map> myData = new ArrayList<Map>();         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);    mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);         PackageManager pm = getPackageManager();    List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);         ... ...    for (int i = 0; i < len; i++) {    ...    if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {    addItem(myData, nextLabel, activityIntent(    info.activityInfo.applicationInfo.packageName,    info.activityInfo.name));    } else {    if (entries.get(nextLabel) == null) {    addItem(myData, nextLabel,    browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));    entries.put(nextLabel, true);    }    }    }    }         Collections.sort(myData, sDisplayNameComparator);         return myData;    }

它通過PackageManager 從  AndroidManifest.xml中讀取所以Intent-Filter含有:Intent.ACTION_MAIN和 Intent.CATEGORY_SAMPLE_CODE所有Activity信息。前面說過200多個示例根據(jù)其功能分類,比如 Hello  World示例它的Label為

App/Activity/<b>Hello <i>World</i></b>,

表示它的分類為分類App下Activity子類。getData(String  prefix)根據(jù)每個Activity的Label屬性和當前層次(prefix)來決定當前列表中某項為葉子列表項,還是分類列表項,如果是葉子列表 項,則添加為activityIntent,當用戶點擊改列表項時則會觸發(fā)該示例。若是分類列表項,則添加為 browseIntent,browseIntent還是觸發(fā)ApiDemos Activity,但Intent帶有Extra信息,表示需要顯示改分類下的子類:

Intent result = new Intent();    result.setClass(this, ApiDemos.class);    result.putExtra("com.example.android.apis.Path", path);

此時如果用戶點擊改節(jié)點列表項,則會進入還分類下級目錄。

protected void addItem(List<Map> data, String name, Intent intent) {    Map<String, Object> temp = new HashMap<String, Object>();    temp.put("title", name);    temp.put("intent", intent);    data.add(temp);    }         @Override   protected void onListItemClick(ListView l, View v, int position, long id) {    Map map = (Map) l.getItemAtPosition(position);         Intent intent = (Intent) map.get("intent");    startActivity(intent);    }

addItem 給返回的List中添加一項,每個記錄含兩列:“title”,“intent”  ,其中只顯示“title”列,如果想要顯示“intent”列的信息,就不能使用 android.R.layout.simple_list_item_1 了,這時可以另外定義一個Layout 來顯示多列數(shù)據(jù)。  intent可以為觸發(fā)示例,如”Hello World”或是下級示例列表,此時觸發(fā)的Activity還是ApiDemos。

此外,ApiDemo還定義了ApiDemosApplication做為Application的子類,如果需要在多個Activity共享一些數(shù)據(jù), 可以定義在Application中。如果使用了自定義的Application,別忘了修改AndroidManifest.xml ,如下:

  1. <application android:name=”ApiDemosApplication”    

  2. android:label=”@string/activity_sample_code”    

  3. android:icon=”@drawable/app_sample_code” >   

  4. &hellip;    

  5. </application>  

上述內(nèi)容就是Android ApiDemo中如何理解ListActivity,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI