Android简明开发教程十:数据绑定Data Binding

jerry Android 2015年08月24日 收藏

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

使用数据显示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来显示绑定的数据。