Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ListView custom row with multiple buttons troubles with OnClickListener it affects multiple list items
    primarykey
    data
    text
    <p>Hello I have searched the forum for hours now and decided to place a question cause I can't really find what's wrong in my code. I have a ListView followed by a custom adapter. Each item in my listView looks like this "|txtView| |Btn 0| |Btn 1| |Btn 2|" and I'm using ViewHolder to increase performance. I use setOnClickListener from the custom adapter. Each button clicked should change it's background to Green color and the others to gray. </p> <p>My <strong>problem</strong> is that when clicking on a certain button at certain row item it also <strong>changes</strong> the background on another button at another row. I don't seem to find my problem , i'm guessing it's related to the fact i'm using the reuse ability with the ViewHolder. </p> <p>Hope you guys can help , many thanks. </p> <p>this is my getView inside the adapter</p> <pre><code>@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.bet_list_item, null); holder = new ViewHolder(); holder.tvGameDescription = (TextView) convertView .findViewById(R.id.gameDescription); holder.button0 = (Button) convertView .findViewById(R.id.button0); holder.button1 = (Button) convertView .findViewById(R.id.button1); holder.button2 = (Button) convertView .findViewById(R.id.button2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } MyOnclickListener myOnclickListener = new MyOnclickListener(holder); holder.buttonSide1.setOnClickListener(myOnclickListener); holder.buttonSideX.setOnClickListener(myOnclickListener); holder.buttonSide2.setOnClickListener(myOnclickListener); </code></pre> <p>this is the listener implementation : </p> <pre><code>private class MyOnclickListener implements OnClickListener { private ViewHolder viewHolder; boolean[] buttonsClickStatus = { false, false, false }; //all gray at start and not clicked public MyOnclickListener(ViewHolder viewHolder) { this.viewHolder = viewHolder; } @Override public void onClick(View v) { switch (((Data) v.getTag()).getBtnPosition()) { case Consts.BUTTON_0: if (!buttonsClickStatus[0]) { // case the btn is gray unclicked setButtonsaBackground(0); // changes the background buttonsClickStatus[0] = true; buttonsClickStatus[1] = false; buttonsClickStatus[2] = false; } else { // case already green clicked already addOrRemove = false; setButtonsaBackground(3); for (int i = 0; i &lt; buttonsClickStatus.length; i++) { buttonsClickStatus[i] = false; } } break; case Consts.BUTTON_1: if (!buttonsClickStatus[1]) { // case gray setButtonsaBackground(1); buttonsClickStatus[1] = true; buttonsClickStatus[0] = false; buttonsClickStatus[2] = false; } else { // case already green addOrRemove = false; setButtonsaBackground(3); for (int i = 0; i &lt; buttonsClickStatus.length; i++) { buttonsClickStatus[i] = false; } } break; case Consts.BUTTON_2: if (!buttonsClickStatus[2]) { // case gray setButtonsaBackground(2); buttonsClickStatus[2] = true; buttonsClickStatus[0] = false; buttonsClickStatus[1] = false; } else { // case already green addOrRemove = false; setButtonsaBackground(3); for (int i = 0; i &lt; buttonsClickStatus.length; i++) { buttonsClickStatus[i] = false; } } break; default: break; } //call a function to update data only in the activity myActivity.update((Data) v.getTag()); } </code></pre> <p>SetBackground is inside the listener as private method: </p> <pre><code> private void setButtonsaBackground(int clicked) { switch (clicked) { case 0: viewHolder.button0.setBackgroundColor(Color.GREEN); viewHolder.button1.setBackgroundColor(Color.GRAY); viewHolder.button2.setBackgroundColor(Color.GRAY); break; case 1: viewHolder.button1.setBackgroundColor(Color.GREEN); viewHolder.button0.setBackgroundColor(Color.GRAY); viewHolder.button2.setBackgroundColor(Color.GRAY); break; case 2: viewHolder.button2.setBackgroundColor(Color.GREEN); viewHolder.button0.setBackgroundColor(Color.GRAY); viewHolder.button1.setBackgroundColor(Color.GRAY); break; case 3: viewHolder.button2.setBackgroundColor(Color.GRAY); viewHolder.button0.setBackgroundColor(Color.GRAY); viewHolder.button1.setBackgroundColor(Color.GRAY); break; default: break; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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