javaandroidandroid-listviewandroid-switch

Android ListView with Switch buttons - only one switch active at a time


I have a list view with two switches. I want the functionality to work were only one switch may be active at a time.

--UPDATED

My Adapter:

 public class NotificationsAdapter extends ArrayAdapter<String> {

private Context context;
private String mTitle[];
private boolean onOff[];

public NotificationsAdapter(Context c, String mTitle[], boolean onOff[])   {
    super(c, R.layout.adapter_notifications_layout, R.id.notificationsListTv, mTitle);
    this.context = c;
    this.mTitle = mTitle;
    this.onOff = onOff;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.adapter_notifications_layout, parent, false);

    Switch notificationSwitch = view.findViewById(R.id.switchNotificationsDaily);
    TextView myTitle = view.findViewById(R.id.notificationsListTv);

    myTitle.setText(mTitle[position]);
    notificationSwitch.setChecked(onOff[position]);

    view.setClickable(true);
    view.setFocusable(true);
    view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int size = onOff.length;
            for (int i = 0; i < size; i++) {
                if (i == position) {
                    break;
                }
                onOff[i] = false;
            }

            if (onOff[position]) {
                notificationSwitch.setChecked(false);
                onOff[position] = false;
            } else {
                onOff[position] = true;
                notificationSwitch.setChecked(true);
            }
        }

    });

    return view;
}

My Class:

   private String[] mTitle = new String[]{"Once Daily", "Twice Daily"};
   private Switch notificationSwitch;
   private boolean[] onOff = new boolean[] {false, false};


     NotificationsAdapter notificationsAdapter = new NotificationsAdapter(getActivity(), mTitle, onOff);
    listView = view.findViewById(R.id.notificationsListView);
    listView.setAdapter(notificationsAdapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.setItemsCanFocus(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
  }

Currently with this code I can select both switches to be active at the same time. When I select more than one switch I would like the other to deactivate. Any assistance getting this functionality to work would be greatly appreciated.


Solution

  • In your activity:-

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            resetValues(position);
    
            if (onOff[position]) {
                onOff[position] = false;
            } else {
                onOff[position] = true;
            }
            notifyDataSetChanged();
        }
    }
    
    private void resetValues(int selectedPosition){
        int size = onOff.length;
        for(int i=0; i < size; i++){
            if(i == selectedPosition){
               continue;
            }
            onOff[i] = false;
        }
    }
    

    And in your adapter:-

    notificationSwitch.setChecked(onOff[position]);
    

    Also, remove your click listener from adapter.

    The logic is:- Making all other values as false except the selected item, then changing the state of the selected item based on its previous state.