roostaamir roostaamir - 5 months ago 52
Android Question

How to disable all the buttons in ListView items when click on button that is inside ListView in android

I have a list view and inside each list-item I have a

TextView
and a
Button
like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin"
android:gravity="right">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button_listItem_action"
android:layout_gravity="center"
android:layout_weight="1.2"
android:layout_marginRight="10dp"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView_listItem_provider_address"
android:layout_gravity="center"
android:layout_weight="2"
android:layoutDirection="rtl"
android:textDirection="rtl"/>

</LinearLayout>


What I'd want to do is that I want to set a click listener for each button in each list-item that disables all other buttons in other list-items in this list view. I don't know if I should put this listener in the adapter or not and I have no idea how to work this.

Here's my adapter:

public class ProviderListArrayAdapter extends ArrayAdapter<Provider> {

private static class ViewHolder{
TextView txtViewAddress;
Button buttonAction;
}

public ProviderListArrayAdapter(Context context, List<Provider> providers) {
super(context, -1, providers);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
final Provider provider = getItem(position);

if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(R.layout.list_item_provider_selector, parent, false);
viewHolder.txtViewAddress = (TextView) convertView.findViewById(R.id.textView_listItem_provider_address);
viewHolder.buttonAction = (Button) convertView.findViewById(R.id.button_listItem_action);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.txtViewAddress.setText(provider.address);
viewHolder.buttonAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//don't know what to do
}
});

return convertView;
}

}


ps: provider class is has just a string text for address.

Any help would be seriously appreciated.Thanks in advance

Answer

You need to keep track of what the last clicked button was. So have a variable in your class

int mLastSelected = -1;  //This is the position last selected or -1 if none is selected

Then in your getView, call

holder.buttonAction.setEnabled(mLastSelected == -1 || mLastSelected == position);

That will disable it if any other button was selected. Then your onClick becomes

mLastSelected = position;
notifyDataSetChanged();

That will write the last selected variable and force the entire list to redraw itself. When that happens all the buttons but the one you just pressed will become disabled.

Comments