سؤال

I implemented a custom Adapter to create a dialog box that displays info related with locations (each entry of the dialogue consists of an image, a text field to display the address and a text field to show the city and country .) In the getView (...) method of the adapter besides using the ViewHolder pattern I use the SoftReference class to save a reference to the views created so the GC can be eliminated any of them before an OutOfMemoryError occurs. My goal is to build a faster and efficient cache. Below the code of my custom Adapter:

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?

Thanks in advance for your answers :D

هل كانت مفيدة؟

المحلول

As far as I can tell, there's no point in using SoftReferences here. If you're correctly recycling views (seems like you are), you will only have a few instances of LocationItemHolder, which will always be the same (within the same adapter). The only time they will become invalidated is when the adapter ceases to be used.

نصائح أخرى

As said, there is no need to use SoftReferences.

Are you having a problem with your application causing OutOfMemory errors? If not there's no point trying to fix something that isn't broken.

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top