data0中的每一个字符串,都显示在R.layout.switcher_view里面(这里R.layout.switcher_view只是个模板,每一页的view是不同的)。如果你的data0不是String类型,应该会崩溃,不信你可以试试(我看过另一种list的源码,是将模板类强转为String的),除非你重写某些接口,自己产生view
解决方案 »
- imageView 设置背景的问题,怎么样居中不拉伸
- spinner问题 大家都来看看啊
- bindservice同步问题
- 有关canvas 类的两个函数的问题
- layout_width
- 为什么安卓下面的视频播放器都没有快速播放和慢速播放功能(不是拖动跳到某个时间点),有没有开源的视频播放器代码可参考?
- 在eclipse上运行安卓源码的时候报错
- android 小程序,求大神调试,,高分
- 我想让Andrioid输出HelloWorld但却无法显示
- include布局后怎么在xml里修改text内容?
- 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