Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvise for performance when using custom Adapter and SoftReference in Android
    primarykey
    data
    text
    <p>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:</p> <pre><code>public class LocationsAdapter extends ArrayAdapter&lt;LocationInfo&gt; { Context context; int layourResourceId; List&lt;LocationInfo&gt; locations; public LocationsAdapter(Context context, int layourResourceId, List&lt;LocationInfo&gt; 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 &amp;&amp; ((SoftReference&lt;LocationItemHolder&gt;) row.getTag()).get() != null) { holder = (LocationItemHolder) ((SoftReference&lt;LocationItemHolder&gt;) 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&lt;LocationItemHolder&gt;(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; } } </code></pre> <p>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 <strong>(LocationItemHolder) ((SoftReference ) row.getTag ()).get()</strong> 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?</p> <p>Thanks in advance for your answers :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload