Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple VIewHolder pattern implementation bug using convertView
    primarykey
    data
    text
    <p>I'm trying to implement the ViewHolder pattern with convertView. Two questions:</p> <p>1) When I comment lines #1 and #2 (which are required for the pattern) everything works fine. When the if is in place everything gets scrambled, the first element of the list gets shown twice (in the beginning and in the end of the list) and after some orientation changes and list scrolling everything gets jumbled. Why is this happenning?</p> <p>2) I'm using a ListActivity and providing the TextView and array of Strings for the ArrayAdapter (#3) but for some reason I still need line (#4) otherwise the list items are blank. Is this because i'm not using super.getView()?</p> <pre><code>class SushiAdapter extends ArrayAdapter&lt;String&gt; { private final Activity context; SushiAdapter(Activity context) { super(context, R.layout.row, R.id.label, MenuItems); // #3 this.context = context; } public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; if (row == null) { //#1 LayoutInflater inflater = context.getLayoutInflater(); row = inflater.inflate(R.layout.row, parent, false); ViewHolder holder = new ViewHolder(); holder.icon = (ImageView) row.findViewById(R.id.icon); holder.position = position; holder.item = (TextView) row.findViewById(R.id.label); row.setTag(holder); } // #2 ViewHolder newHolder = (ViewHolder) row.getTag(); newHolder.item.setText(MenuItems[newHolder.position]); // #4 newHolder.icon.setImageResource(R.drawable.sushi); return row; } } </code></pre>
    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.
 

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