Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Change Button background in ListView Row with onClick
    text
    copied!<p>My rows contain a button that has its own click listener set in my adapter's getView. I'm able to distinguish between my button clicks and the actual row item clicks using android:descendantFocusability="blocksDescendants" in the row's parent.</p> <p>When I click on a button it sets the button background properly, my problem is as I scroll through the list its setting it for different rows as well. I assume theirs an issue somewhere with views recycling. </p> <p>Here's my code:</p> <pre><code>@Override public View getView(int position, View convertView, ViewGroup parent){ if(convertView == null){ holder = new ViewHolder(); convertView = inflater.inflate(R.layout.todays_sales_favorite_row, null); holder.favCatBtn = (Button)convertView.findViewById(R.id.favCatBtn); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.favCatBtn.setTag(position); holder.favCatBtn.setOnClickListener(this); return convertView; } @Override public void onClick(View v) { int pos = (Integer) v.getTag(); Log.d(TAG, "Button row pos click: " + pos); RelativeLayout rl = (RelativeLayout)v.getParent(); holder.favCatBtn = (Button)rl.getChildAt(0); holder.favCatBtn.setBackgroundResource(R.drawable.icon_yellow_star_large); } </code></pre> <p>So if i click on the button at row position 1 the button background changes as it should. But then as i scroll down the list random other buttons are getting set as well. Then sometimes when I scroll back up to position 1 the button background reverts back to the original again.</p> <p>What am I missing here? I know I'm right there its just something minor I'm not doing. </p>
 

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