Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ListActivity in Android ApiDemo

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to understand ListActivity in Android ApiDemo. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Once the ApiDemo project is created, we can analyze each sample code.

First of all, take a look at the main Activity:com.example.android.apis.ApiDemos of ApiDemo, which is a subclass of ListActivity and is mainly used to list more than 200 instances in ApiDemos. The examples are displayed at a classification level.

The code in onCreate () of ApiDemos:

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

As a bridge between data source getData (path) and UI ListActivity, SimpleAdatper has the following constructors:

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

We know that ListActivity can be used to display a list, and when using SimpleAdapter, you can borrow a two-dimensional table for better understanding. The data source data type of SimpleAdapter is List > List. Each item is a Map object, which is equivalent to a row in a two-dimensional table. This row can have multiple columns, and each column can have a name of Map string, which is equivalent to the column name of the table:

Only one column of "title" is displayed for each record in ApiDemos. Android.R.layout.simple_list_item_1 is the Layout resource id used to display each record. ListActivity allows the use of custom Layout. Here the Android system resource is used. Simple_list_item_1 consists of a TextView, and its id is text1.

New String [] {"title"} is an array of lists to be displayed. ApiDemos displays only one column of "title". If there are multiple columns: new String [] {"title", "field1", "field2", "field3"}.

New int [] {android.R.id.text1} specifies that the TextView in android.R.layout.simple_list_item_1 with id as text1 is used to display the "title" column. If there are multiple columns, Layout can define multiple View (not necessarily TextView), and then specify the id of the displayed View for each column.

Let's look at how getData (path) is defined, and protected List getData (String prefix) returns a list.

Protected List getData (String prefix) {List myData = new ArrayList (); Intent mainIntent = new Intent (Intent.ACTION_MAIN, null); mainIntent.addCategory (Intent.CATEGORY_SAMPLE_CODE); PackageManager pm = getPackageManager (); List list = pm.queryIntentActivities (mainIntent, 0); For (int I = 0; I < len; iTunes +) {... 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;}

It reads from AndroidManifest.xml through PackageManager, so Intent-Filter contains all the Activity information: Intent.ACTION_MAIN and Intent.CATEGORY_SAMPLE_CODE. As mentioned earlier, more than 200 examples are classified according to their functions, such as the Hello World example, whose Label is

App/Activity/Hello World

Indicates that it is classified as an Activity subcategory under classification App. GetData (String prefix) determines whether an item in the current list is a leaf list item or a classified list item based on the Label attribute of each Activity and the current prefix. If it is a leaf list item, it is added as activityIntent, and the example is triggered when the user clicks on the change list item. If the list item is classified, it is added as browseIntent,browseIntent or ApiDemos Activity is triggered, but Intent has Extra information, indicating that the subcategories under the reclassification need to be displayed:

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

At this point, if the user clicks the change node list item, it will enter the subcategory directory.

Protected void addItem (List data, String name, Intent intent) {Map temp = new HashMap (); 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 adds an item to the returned List, and each record contains two columns: "title" and "intent", in which only the "title" column is displayed. If you want to display the information in the "intent" column, you cannot use android.R.layout.simple_list_item_1. In this case, you can define another Layout to display multiple columns of data. Intent can be a trigger example, such as "Hello World" or a list of subordinate examples, in which case the triggered Activity is still ApiDemos.

In addition, ApiDemo also defines ApiDemosApplication as a subclass of Application, which can be defined in Application if you need to share some data among multiple Activity. If you use a custom Application, don't forget to modify the AndroidManifest.xml as follows:

...

The above is how to understand ListActivity in Android ApiDemo. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report