Android简明开发教程十:数据绑定Data Binding
来源:互联网 发布:淘宝开店认证 上半身 编辑:程序博客网 时间:2024/06/05 04: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 中相关代码:
public
class
AndroidGraphics2DTutorial
extends
ListActivity {
private
static
final
String SAMPLE_CATEGORY
=
"com.pstreets.graphics2d.SAMPLE_CODE"
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setListAdapter(
new
SimpleAdapter(
this
, getData(),
android.R.layout.simple_list_item_1,
new
String[] {
"title"
},
new
int
[] { android.R.id.text1 }));
getListView().setTextFilterEnabled(
true
);
}
protected
List getData() {
List<Map> myData =
new
ArrayList<Map>();
Intent mainIntent =
new
Intent(Intent.ACTION_MAIN,
null
);
mainIntent.addCategory(SAMPLE_CATEGORY);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent,
0
);
if
(
null
== list)
return
myData;
String[] prefixPath;
prefixPath =
null
;
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;
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(nextLabel));
entries.put(nextLabel,
true
);
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return
myData;
}
private
final
static
Comparator<Map> sDisplayNameComparator
=
new
Comparator<Map>() {
private
final
Collator collator = Collator.getInstance();
public
int
compare(Map map1, Map map2) {
return
collator.compare(map1.get(
"title"
), map2.get(
"title"
));
}
};
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
, AndroidGraphics2DTutorial.
class
);
return
result;
}
protected
void
addItem(List<Map> 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
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);
}
}
使用数据显示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来显示绑定的数据。
- Android简明开发教程十:数据绑定Data Binding
- Android简明开发教程十:数据绑定Data Binding
- Android Data Binding(数据绑定)教程汇总
- Android Data Binding 数据绑定技术导读
- Android数据绑定Data Binding初体验
- Android Data Binding(数据绑定)用户指南
- Android Data Binding(数据绑定)用户指南
- Android 数据绑定(Data Binding)详解
- android数据绑定之Data Binding
- 数据绑定(Data Binding)
- 数据绑定(data binding)
- Android数据绑定库Data Binding Library介绍
- 数据绑定数据库详解Android Data Binding Library
- vlc for android中的数据绑定(Data Binding)
- Android Data Binding(结合RecyclerView动态绑定数据)
- Android 官方数据绑定框架 Data Binding 简单使用
- HTML中的数据绑定(Data Binding)
- 数据绑定(data binding )1
- 对KMP算法的理解
- JAVA操作字符集
- Android简明开发教程九:创建应用程序框架
- hdu 2037 今年暑假不AC(贪心)
- MySQL5中文乱码解决5
- Android简明开发教程十:数据绑定Data Binding
- lca rmq 算法资料
- Android简明开发教程十一:自定义Adapter显示列表
- lucene2.0+heritrix
- Android简明开发教程十二:引路蜂二维图形库简介及颜色示例
- Android简明开发教程十三:Option Menu 画笔示例
- Android简明开发教程十四:Context Menu 绘制几何图形
- 利用jsoup 如何从网页中下载图片
- Android简明开发教程十五:RadioButton 多边形及路径绘制