Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looking inside the working of ListViews and ListAdapters, I came to know there's a lot of recycling of objects, specifically List View Item Objects which adapters produce. Here are the lessons learnt along with solution to Original problem:</p> <ul> <li><p>When a <code>ListView</code> has to draw/show a list item, it requests a <code>View</code> from <code>ListAdapter</code>, and some times (NOT ALWAYS) also provides an old <code>View</code> object to reuse. This reuse of objects is there so as to increase performance,there's an in built <em>Re-Cycler</em> in <code>ListView</code> to do this, why inflate new layouts for each new list item, when there are already some whose properties can be modified so that they look like the new view item. Until this point , its OK for adapter to change some text on old views and give them back, or create new ones if no recycled views are available, or even discard recyclable views and always create new one.</p></li> <li><p>However, If your List Item's state is more than just some text in a <code>TextView</code> , that is, another object is registered as an <code>onClickListener</code> for your list item, or your list item has a reference to some object somewhere and vice-verca; it is NOT OK for adapter to just change appearance of the reusable Views or simply discard them. <strong>Adapter has to update entire state of a reusable item.</strong> that includes de-registering old event listeners, re-registering new ones and updating all reference to external objects that may be there.</p></li> </ul> <p>Changed <code>getView()</code> method for adapter to:</p> <pre><code>@Override public View getView(int position, View convertView, ViewGroup parent) { Item item = getItem(position); Log.d("My_Test","Adapter : View for Item: " + item.getName() +"is requested." ); if(convertView != null) { (convertView.findViewById(R.id.deleteButton)) .setOnClickListener(item); ((TextView)convertView.findViewById(R.id.nameTextView)) .setText(item.getName()); return convertView; } else { return item.getView(); } } </code></pre> <p>NOTE: While always creating new items in this case does not cause any errors, the <code>ListView</code> fails to detect changes and redraw. Making use of recycled items seems to solve this.</p>
    singulars
    1. This table or related slice is empty.
    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