我安装了一台调音器,以建立一个显示与地点有关的胎儿的方言箱(对话的每一条目都包括图像、显示地址的文字场和展示城市和国家的文本场)。 在适应器的收听(......)方法中,除了使用“观点”模式一外,还使用“软背”类来避免提及所形成的观点,以便在出现“外部”之前消除任何一种观点。 我的目标是建立一个更快、高效的海滩。 遵循我的习俗改编者守则:
public class LocationsAdapter extends ArrayAdapter<LocationInfo> {
Context context;
int layourResourceId;
List<LocationInfo> locations;
public LocationsAdapter(Context context, int layourResourceId,
List<LocationInfo> locations) {
super(context, layourResourceId, locations);
this.context = context;
this.layourResourceId = layourResourceId;
this.locations = locations;
}
@Override
public View getView(int position, View row, ViewGroup parent) {
LocationItemHolder holder = null;
if (row != null && ((SoftReference<LocationItemHolder>) row.getTag()).get() != null) {
holder = (LocationItemHolder) ((SoftReference<LocationItemHolder>) row.getTag()).get();
} else {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layourResourceId, parent, false);
holder = new LocationItemHolder();
holder.imgMarker = (ImageView) row.findViewById(R.id.imgMarker);
holder.txtNameStreet = (TextView) row
.findViewById(R.id.txtNameStreet);
holder.txtRegion = (TextView) row.findViewById(R.id.txtRegion);
row.setTag(new SoftReference<LocationItemHolder>(holder));
}
LocationInfo location = locations.get(position);
holder.imgMarker.setImageResource(location.getMarkerId());
holder.txtNameStreet.setText(location.getNameStreet());
holder.txtRegion.setText(location.getRegion());
return row;
}
class LocationItemHolder {
ImageView imgMarker;
TextView txtNameStreet;
TextView txtRegion;
}
}
I am very interested in making things as efficient as possible. Although the code make what I want I m not sure if I m making good use SoftReference class. For example the sentence (LocationItemHolder) ((SoftReference ) row.getTag ()).get() I think it makes the cache ineffective because of the number of methods to be called to retrieve the desired object each time the method getView is invoked, also requires multiple castings. Can that sentence make inefficient the cache?. Is it advisable to use SoftReference in the context of an adapter in Android?
提前感谢您的答复:D