成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android開發速成簡潔教程十:數據綁定Data Binding

移動開發 Android
前面提到AndroidGraphics2DTutorial說過它是ListActivity派生出來的。ListActivity中顯示的是 ListView,ListView和Gallery ,Spinner有一個共同點:它們都是AdapterView的子類。

前面提到AndroidGraphics2DTutorial說過它是ListActivity派生出來的。ListActivity中顯示的是 ListView,ListView和Gallery ,Spinner有一個共同點:它們都是AdapterView的子類。AdapterView的顯示可以通過數據綁定來實現,數據源可以是數組或是數據庫記錄,數據源和AdapterView是通過Adapter作為橋梁。通過Adapter,AdatperView可以顯示數據源或處理用戶選取時間, 如:選擇列表中某項。

AndroidGraphics2DTutorial讀取AndroidManifest.xml中Intent-Filter為

<action android:name=”android.intent.action.MAIN” />
<category android:name=”com.pstreets.graphics2d.SAMPLE_CODE” />

的所有Activity,以列表方式顯示。使用了Android API 自帶的SimpleAdapter。 來看看AndroidGraphics2DTutorial.java 中相關代碼:

  1. public class AndroidGraphics2DTutorial extends ListActivity {  
  2.      private static final String SAMPLE_CATEGORY 
  3.               ="com.pstreets.graphics2d.SAMPLE_CODE";      
  4.      @Override 
  5.      public void onCreate(Bundle savedInstanceState) { 
  6.       super.onCreate(savedInstanceState);     
  7.       setListAdapter(new SimpleAdapter(this, getData(), 
  8.         android.R.layout.simple_list_item_1, new String[] { "title" }, 
  9.         new int[] { android.R.id.text1 })); 
  10.       getListView().setTextFilterEnabled(true);  
  11.      }    
  12.      protected List getData() { 
  13.       List<Map> myData = new ArrayList<Map>();    
  14.       Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
  15.       mainIntent.addCategory(SAMPLE_CATEGORY);    
  16.       PackageManager pm = getPackageManager(); 
  17.       List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);   
  18.         if (null == list) 
  19.        return myData;     
  20.       String[] prefixPath;    
  21.       prefixPath = null;      
  22.       int len = list.size();      
  23.       Map<String, Boolean> entries = new HashMap<String, Boolean>();      
  24.       for (int i = 0; i < len; i++) { 
  25.        ResolveInfo info = list.get(i); 
  26.        CharSequence labelSeq = info.loadLabel(pm); 
  27.    String label = labelSeq != null ? labelSeq.toString() 
  28.          : info.activityInfo.name; 
  29.        String[] labellabelPath = label.split("/");     
  30.        String nextLabel = prefixPath == null ? labelPath[0] 
  31.          : labelPath[prefixPath.length];  
  32.        if ((prefixPath != null ? prefixPath.length : 0) 
  33.     == labelPath.length - 1) { 
  34.         addItem(myData, 
  35.           nextLabel, 
  36.           activityIntent( 
  37.             info.activityInfo.applicationInfo.packageName, 
  38.             info.activityInfo.name)); 
  39.        } else { 
  40.         if (entries.get(nextLabel) == null) { 
  41.          addItem(myData, nextLabel, browseIntent(nextLabel)); 
  42.          entries.put(nextLabel, true); 
  43.         } 
  44.        }      
  45.       } 
  46.       Collections.sort(myData, sDisplayNameComparator);   
  47.       return myData; 
  48.      } 
  49.       
  50.      private final static Comparator<Map> sDisplayNameComparator 
  51.     = new Comparator<Map>() { 
  52.       private final Collator collator = Collator.getInstance();   
  53.       public int compare(Map map1, Map map2) { 
  54.        return collator.compare(map1.get("title"), map2.get("title")); 
  55.       } 
  56.      }; 
  57.       
  58.      protected Intent activityIntent(String pkg, String componentName) { 
  59.       Intent result = new Intent(); 
  60.       result.setClassName(pkg, componentName); 
  61.       return result; 
  62.      }    
  63.      protected Intent browseIntent(String path) { 
  64.       Intent result = new Intent(); 
  65.       result.setClass(this, AndroidGraphics2DTutorial.class); 
  66.       return result; 
  67.      }    
  68.      protected void addItem(List<Map> data, String name, Intent intent) { 
  69.       Map<String, Object> temp = new HashMap<String, Object>(); 
  70.       temp.put("title", name); 
  71.       temp.put("intent", intent); 
  72.       data.add(temp); 
  73.      }    
  74.      @Override 
  75.      protected void onListItemClick(ListView l, View v, 
  76.         int position, long id) { 
  77.       Map map = (Map) l.getItemAtPosition(position); 
  78.       Intent intent = (Intent) map.get("intent"); 
  79.       startActivity(intent); 
  80.      } 
  81.     } 

使用數據顯示Layout,上面代碼中

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

為ListActivity中ListView 指定Adapter,這個Adapter的數據源為getData(),getData()從Manifest.xml中查找出所有符合條件的示例 Activity列表。 這里DataSource是靜態的從文件中讀取,如果DataSource為數組或是其它數據源,如果程序中修改數值的內容,則你應該 notifyDataSetChanged()來通知UI數據有變動。UI則會刷新顯示以反映數據變化。簡單的說Android數據綁定和.Net WinForm ,WPF 中數據綁定類似。

處理用戶選取事件,AdapterView.OnItemClickListener()可以用來處理選取事 件,對于ListActivity,可以用protected void onListItemClick(ListView l, View v, int position, long id)。AndroidGraphics2DTutorial中的實現是用戶選取Activity名稱好,則啟動對應的Activity。

上面代碼中使用SimpleAdapter,并使用Android提供的android.R.layout.simple_list_item_1 來顯示數據,Andrid也允許使用自定義的Layout來顯示數據,對這個例子來說,可以使用圖片加說明來顯示列表,將在后面介紹如果使用自定義 Adapter和自定義Layout來顯示綁定的數據。

責任編輯:閆佳明 來源: imobilebbs
相關推薦

2013-12-26 15:10:08

Android開發應用和框架Linux 內核

2013-12-26 15:43:07

Android開發Android應用Activities

2013-12-26 15:18:09

Android開發安裝開發環境

2013-12-27 14:34:46

Android開發Android應用短信觸發示例

2013-12-27 14:05:22

Android開發Android應用Dialog

2013-12-27 14:16:43

Android開發Android應用線程

2013-12-27 12:51:44

Android開發Android應用引路蜂

2013-12-27 13:49:22

Android開發Android應用Button

2013-12-26 16:24:13

Android開發Android應用Intents

2013-12-27 13:27:05

Android開發Android應用RadioButton

2013-12-27 16:06:10

Android開發Android應用發布應用

2013-12-26 15:46:30

Android開發Android應用用戶界面設計

2013-12-26 15:34:19

Android開發Android應用基本概念

2013-12-26 16:46:21

2013-12-27 15:31:26

Android開發Android應用資源Resources

2015-08-11 09:41:58

AndroiddataBinding

2013-12-26 17:08:36

Android開發Android應用自定義Adapter顯

2013-12-27 13:00:30

Android開發Android應用Context Men

2013-12-27 12:42:15

Android開發Android應用引路蜂

2013-12-27 14:10:36

Android開發Android應用Transform
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 6080yy精品一区二区三区 | 这里精品 | 精品久久久久久中文字幕 | 久久免费香蕉视频 | 无码日韩精品一区二区免费 | 中文字幕免费在线观看 | 成人免费视频7777777 | 亚洲一区二区在线视频 | 四虎永久免费黄色影片 | 麻豆一区二区三区精品视频 | 亚洲人人 | 国产欧美日韩在线观看 | 日韩精品一区二区三区中文在线 | 久久五月婷 | 国产精品欧美一区二区三区 | 一级黄色毛片免费 | 亚洲欧美一区二区三区国产精品 | 三级av网址 | www.亚洲一区二区三区 | 在线观看视频91 | 你懂的av | 国产一区二区三区四区五区加勒比 | 91久久久久久久久久久 | 香蕉视频久久久 | 精品日韩一区 | 欧美日韩国产一区二区三区 | 国外成人在线视频 | 99精品在线 | 9191av| 精品国产乱码久久久久久图片 | 欧美一区二区大片 | 91精品国产一二三 | 91不卡在线 | 日韩精品一区二区久久 | 国产亚洲一区二区三区在线观看 | 国产成人自拍av | 精品一区二区三区在线观看国产 | 一本大道久久a久久精二百 国产成人免费在线 | 亚洲国产网址 | 国产精品综合色区在线观看 | 成人精品一区二区 |