Note that there are some explanatory texts on larger screens.

plurals
  1. POManipulating a ListView's items contained in a ViewHolder - Unexpected behaviour
    primarykey
    data
    text
    <p>I am experiencing some weird behaviour in my code you'll find below. I have a button in each row of my <code>ListView</code> and if it is clicked, I want to gray out the row and hide the button. To make the change permanent I have set a property in the custom <code>ViewHolder</code> so the changes are persistent.</p> <p>However, clicking the button on, say item 1, will also apply the changes to item 6. On other items it affects no other row or it affects more than one row. I suspect it has something to do with me declaring some <code>final</code> variables but I cannot be sure. I tried implementing different solutions (reducing the number of final variables, using different variables, ect) but nothing really seems to work.</p> <p>I am in need of some assistance. Thank you!</p> <pre><code>public class FruitLoopsAdapter extends BaseAdapter { private ArrayList&lt;FruitLoopsNode&gt; provider; private Activity parent; /** * Constructor. * @param parent * The parent activity of the fragment using the FruitLoop. Needed as the adapter will * be used in a fragment which only its parents has access to the data providers. */ public FruitLoopsAdapter(Activity parent) { this.parent = parent; provider = ((Box)parent).getFruitLoops(); } /** * Method returning the size of the underlying data provider. * @return * The size of the data provider. * @see android.widget.Adapter#getCount() */ @Override public int getCount() { return provider.size(); } /**{@inheritDoc}*/ @Override public Object getItem(int position) { return provider.get(position); } /**{@inheritDoc}*/ @Override public long getItemId(int position) { return position; } /** * Method building the composite custom view and returning it as one view. * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public View getView(int position, View convertView, ViewGroup parentViewGroup) { FruitLoopsViewHolder viewHolder; final FruitLoopsNode FruitLoopsNode = provider.get(position); if (convertView == null) { convertView = LayoutInflater.from(parentViewGroup.getContext()).inflate(R.layout.list_FruitLoops, null); viewHolder = new FruitLoopsViewHolder( (TextView) convertView.findViewById(R.id.time), (Button) convertView.findViewById(R.id.consume), (ImageView) convertView.findViewById(R.id.thumbnail), (TextView) convertView.findViewById(R.id.content_title), (TextView) convertView.findViewById(R.id.content_brief), (RatingBar) convertView.findViewById(R.id.ratingBar)); convertView.setTag(viewHolder); } else { viewHolder = (FruitLoopsViewHolder) convertView.getTag(); determinePresentationMode(convertView, viewHolder); } /** [Omitted some code setting many of the fields above to] */ final Button button = viewHolder.getButton(); final View finalConvertView = convertView; button.setOnClickListener(new OnClickListener() { public void onClick(View view) { button.setEnabled(false); button.setVisibility(View.INVISIBLE); finalConvertView.setAlpha(0.5f); finalConvertView.setBackgroundColor(Color.GRAY); } }); /** * As using a final viewHolder to set the value inside the onClick() method of the * button will cause the images not to update anymore, investigating the alpha value * of the convertView seemed the only easy way to get there. Also, as it is a float, * I want to leave a bit of extra padding in the test. */ if(finalConvertView.getAlpha() &lt; 0.9) { viewHolder.setConsumed(true); } return convertView; } private void determinePresentationMode(View convertView, FruitLoopsViewHolder viewHolder) { if(viewHolder.wasConsumed()) { convertView.setAlpha(0.5f); convertView.setBackgroundColor(Color.GRAY); } } } </code></pre>
    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.
 

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