标签:style ar io os sp for java on 数据
我曾想利用ArrayAdapter中的getView来获取ListView中每个item的View对象。结果是,无论我怎么对View对象进行更改都不产生影响。不会变是理所当然的,因为getView,其实是”createView“。下面是我参考的源代码:
public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } 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; }
从代码来看,肯定不可能是我所想的那样。如果convertView等于null,每次调用getView都是返回一个新的对象。但是,为什么呢?
我的理解是,Adapter作为Controller,向下从Model层中获取数据,向上向View层提供对象。作为给予者,不应该了解View层的信息。所以,通过Adapter来获取View层的对象,这本身就是不可取的。
标签:style ar io os sp for java on 数据
原文地址:http://my.oschina.net/u/582355/blog/356838