zoukankan      html  css  js  c++  java
  • 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考

    01 API Demos

    ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:

    02 入口类: SimpleAdapter,ListActivity,PackageManager的使用

    ApiDemos采用分类层次显示,ApiDemo是ApiDemos的入口类,继承自ListActivity,该类的主要功能是分类列出大类,进而更进一步显示示例。

    ListActivity可以看做为一个含有ListView的Activity,可以不用setContentView设置布局,除非你想定制自己的List布局,这样的话,你的布局中必须包含一个id"@id/android:list"的ListView。 若你还指定了一个id"@id/android:empty"的view,当ListView中没有数据要显示时,这个view就会被显示,同时 ListView会被隐藏。

    ListActivity使用方法  

    ListActivity英文API

    ApiDemo这个类可以作为ListActivity使用的参考示例。

    在ApiDemos 的 onCreate()中代码:

         Intent intent = getIntent();
            String path = intent.getStringExtra("com.example.android.apis.Path");
            
            if (path == null) {
                path = "";
            }
    
            setListAdapter(new SimpleAdapter(this, getData(path),
                    android.R.layout.simple_list_item_1, new String[] { "title" },
                    new int[] { android.R.id.text1 }));
            getListView().setTextFilterEnabled(true);

     这里直接调用ListActivity的setListAdapter给ListView设置适配器。

    SimpleAdapter在这里作为数据源getData(path)与ListView(ListActivity)之间的桥梁,它的构造函数如下:

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

     简约而不简单——Android SimpleAdapter

    使用SimpleAdapter时可以借用二维表来更好的理解,SimpleAdapter的数据源data 类型为List<? extends Map<String, ?>> List 中每一项为一个Map对象,相当于二维表中一行,这一行可以有多列,每列可以有个名字,为Map<String,?> string ,相当于表的列名:

    ApiDemos中每条记录只显示一列”title”。

    android.R.layout.simple_list_item_1 为用来显示每条记录的Layout资源id, ListActivity允许使用自定义Layout ,这里使用了Android系统资源,simple_list_item_1由一个TextView构成,其id为text1.

    new String[] { “title” } 为需要显示的列表的数组,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<Map<String, Object>> getData(String prefix) {
            List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
    
            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
    
            PackageManager pm = getPackageManager();                //这部分通过PackageManager从 AndroidManifest.xml中读取所
            List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);  //有Intent-Filter含有:Intent.ACTION_MAIN和 Intent.CATEGORY_SAMPLE_CODE所有Activity信息
    
            if (null == list)
                return myData;
    
            String[] prefixPath;
            String prefixWithSlash = prefix;
            
            if (prefix.equals("")) {
                prefixPath = null;
            } else {
                prefixPath = prefix.split("/");
                prefixWithSlash = prefix + "/";
            }
            
            int len = list.size();
            
            Map<String, Boolean> entries = new HashMap<String, Boolean>();
    
            for (int i = 0; i < len; i++) {
                ResolveInfo info = list.get(i);
                CharSequence labelSeq = info.loadLabel(pm);
                String label = labelSeq != null
                        ? labelSeq.toString()
                        : info.activityInfo.name;
                
                if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
                    
                    String[] labelPath = label.split("/");
    
                    String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
    
                    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信息。前面说过数百个示例根据其功能分类,比如 Hello World示例它的Label为

    App/Activity/<b>Hello <i>World</i></b>,表示它的分类为分类App下Activity子类。

            <!-- Activity Samples -->
    
            <activity android:name=".app.HelloWorld" android:label="@string/activity_hello_world">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.SAMPLE_CODE" />
                </intent-filter>
            </activity>
    <string name="activity_hello_world">App/Activity/<b>Hello <i>World</i></b></string>

    getData(String prefix)根据每个Activity的Label属性和当前层次(prefix)来决定当前列表中某项为叶子列表项,还是分类列表项,如果是叶子列表项,则添加为activityIntent,当用户点击改列表项时则会触发该示例。若是分类列表项,则添加为 browseIntent,browseIntent还是触发ApiDemos Activity,但Intent带有Extra信息,表示需要显示该分类下的子类:

        protected Intent activityIntent(String pkg, String componentName) {
            Intent result = new Intent();
            result.setClassName(pkg, componentName);
            return result;
        }
        
        protected Intent browseIntent(String path) {
            Intent result = new Intent();
            result.setClass(this, ApiDemos.class);
            result.putExtra("com.example.android.apis.Path", path);
            return result;
        }

    此时如果用户点击该节点列表项,则会进入该分类下级目录。

        protected void addItem(List<Map<String, Object>> 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
        @SuppressWarnings("unchecked")
        protected void onListItemClick(ListView l, View v, int position, long id) {
            Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
    
            Intent intent = new Intent((Intent) map.get("intent"));
            intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
            startActivity(intent);
        }

    addItem 给返回的List中添加一项,每个记录含两列 :”title”,”intent” ,其中只显示”title”列,如果想要显示”intent”列的信息,就不能使用 android.R.layout.simple_list_item_1 了,这时可以另外定义一个Layout 来显示多列数据。 intent可以为触发示例,如”Hello World”或是下级示例列表,此时触发的Activity还是ApiDemos。
    此外,ApiDemo还定义了ApiDemosApplication做为Application的子类,如果需要在多个Activity共享一些数据, 可以定义在Application中。如果使用了自定义的Application,别忘了修改AndroidManifest.xml ,如下:

    <application android:name="ApiDemosApplication"
                android:label="@string/activity_sample_code"
                android:icon="@drawable/app_sample_code"
                android:hardwareAccelerated="true"
                android:supportsRtl="true"></application> 
  • 相关阅读:
    企业如何利用大数据?八个典型应用案例告诉你
    无焦点下获取条码枪返回值的Hook(再次改良版)
    combining-filters
    为Elasticsearch添加中文分词,对比分词器效果
    ElasticSearch NEST搜索
    How to write date range query in Nest ElasticSearch client?
    ElasticSearch关联查找
    form-inline+form-group 实现表单横排显示(Bootstrap)
    Elastic Search 5.x Nest Multiple Queries C#
    mysql 查看索引
  • 原文地址:https://www.cnblogs.com/dongdong230/p/4303946.html
Copyright © 2011-2022 走看看