Android开发中如何进行数据绑定Data Binding(android开发,data binding,移动开发)

时间:2024-05-03 03:57:34 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

前面提到AndroidGraphics2DTutorial说过它是ListActivity派生出来的。ListActivity中显示的是 ListView,ListView和Gallery ,Spinner有一个共同点:它们都是AdapterView的子类。AdapterView的显示可以通过数据绑定来实现,数据源可以是数组或是数据库记录,数据源和AdapterView是通过Adapter作为桥梁。通过Adapter,AdatperView可以显示数据源或处理用户选取时间, 如:选择列表中某项。

Android开发中如何进行数据绑定Data Binding

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 中相关代码:

publicclassAndroidGraphics2DTutorialextendsListActivity{privatestaticfinalStringSAMPLE_CATEGORY="com.pstreets.graphics2d.SAMPLE_CODE";@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setListAdapter(newSimpleAdapter(this,getData(),android.R.layout.simple_list_item_1,newString[]{"title"},newint[]{android.R.id.text1}));getListView().setTextFilterEnabled(true);}protectedListgetData(){List<Map>myData=newArrayList<Map>();IntentmainIntent=newIntent(Intent.ACTION_MAIN,null);mainIntent.addCategory(SAMPLE_CATEGORY);PackageManagerpm=getPackageManager();List<ResolveInfo>list=pm.queryIntentActivities(mainIntent,0);if(null==list)returnmyData;String[]prefixPath;prefixPath=null;intlen=list.size();Map<String,Boolean>entries=newHashMap<String,Boolean>();for(inti=0;i<len;i++){ResolveInfoinfo=list.get(i);CharSequencelabelSeq=info.loadLabel(pm);Stringlabel=labelSeq!=null?labelSeq.toString():info.activityInfo.name;String[]labellabelPath=label.split("/");StringnextLabel=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(nextLabel));entries.put(nextLabel,true);}}}Collections.sort(myData,sDisplayNameComparator);returnmyData;}privatefinalstaticComparator<Map>sDisplayNameComparator=newComparator<Map>(){privatefinalCollatorcollator=Collator.getInstance();publicintcompare(Mapmap1,Mapmap2){returncollator.compare(map1.get("title"),map2.get("title"));}};protectedIntentactivityIntent(Stringpkg,StringcomponentName){Intentresult=newIntent();result.setClassName(pkg,componentName);returnresult;}protectedIntentbrowseIntent(Stringpath){Intentresult=newIntent();result.setClass(this,AndroidGraphics2DTutorial.class);returnresult;}protectedvoidaddItem(List<Map>data,Stringname,Intentintent){Map<String,Object>temp=newHashMap<String,Object>();temp.put("title",name);temp.put("intent",intent);data.add(temp);}@OverrideprotectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){Mapmap=(Map)l.getItemAtPosition(position);Intentintent=(Intent)map.get("intent");startActivity(intent);}}

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

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Android开发中如何进行数据绑定Data Binding的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Android中怎么绘制引路蜂二维图形下一篇:

18 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18