data0中的每一个字符串,都显示在R.layout.switcher_view里面(这里R.layout.switcher_view只是个模板,每一页的view是不同的)。如果你的data0不是String类型,应该会崩溃,不信你可以试试(我看过另一种list的源码,是将模板类强转为String的),除非你重写某些接口,自己产生view
解决方案 »
- 关于Fragment的切换状态保存问题
- 如何在应用程序里用手机自带的音量键控制媒体音量?
- 请问代码出错的地方
- 各位大神,小弟我正在上大学,想学Android的开发,需要学哪些东西?看哪些书啊?
- android:layout_marginBottom相对应的代码
- Android 4.3有隐藏功能”App Ops”应用权限管理
- 请教大家在windows下编译和在Linux下编译有啥区别?
- 关于数据库的问题
- 黑马程序中的手机彩票的服务器怎么配置
- genymotion 虚拟机怎么安装不了微信apk?
- Process android.process.media is not respoding
- android 模拟器和真机的性能差别
private View createViewFromResource(int position, View convertView, ViewGroup parent,
int resource) {
View view;
TextView text; if (convertView == null) {
view = mInflater.inflate(resource, parent, false);
} else {
view = convertView;
} try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
} T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
} return view;
}
这是源代码,上面我说可能会崩溃,其实不会(对于ArrayAdapter来说),其中resource就是你传入的layout,由于有了这些代码:
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
就不会崩溃了,如果你的模板参数T是instanceof CharSequence,则直接强转(这没问题),如果不是,则调用Object的toString(这可能不是你想要的,比如说你的T是ListView的话,其toStirng其实是类名)
你不把ArrayAdatper也改成<ListView>,编译都有问题,我说的是运行崩溃。我看过ExpandableListAdapter的源代码,它也带模板参数T,而实际上这个T只能是String类型,因为它是强转String类型的。
这我还发贴问过:
http://topic.csdn.net/u/20110302/18/1528855d-7447-4378-bf90-0d2542f446f3.html