0

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.

AndroidDev123
  • 280
  • 3
  • 24
  • And what is the problem that you are currently facing? Can you clarify it? – AgentP Jun 14 '20 at 15:11
  • When I select one of the switches it isnt disabled the other. Its allowing me to select both on at the same time. – AndroidDev123 Jun 14 '20 at 18:58
  • Can you put a log statement and find at least the setOnItemClickListener on listView working properly ? – AgentP Jun 15 '20 at 06:27
  • You're right, the onItemClickListener is not working. I'm not sure why, its exactly the same code I used for another ListView which works... – AndroidDev123 Jun 15 '20 at 10:24
  • android:descendantFocusability="blocksDescendants" This allows the OnItemClickListener to work, however now its throwing a null on the else: } else { onOff[position] = true; notificationSwitch.setChecked(true); – AndroidDev123 Jun 15 '20 at 10:52
  • notificationSwitch is in the layout for the adapter but the onItemClick is in the class, so its null because its not in the layoutfile for the class its in the layout file for the adapter. How does this work then? – AndroidDev123 Jun 15 '20 at 10:58

1 Answers1

0

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.

Shubham Raitka
  • 1,034
  • 8
  • 15
  • I tried adding this code in however its still allowing me to check both switched on at the same time. – AndroidDev123 Jun 14 '20 at 18:32
  • android:descendantFocusability="blocksDescendants" This allows the OnItemClickListener to work, however now its throwing a null on the else: } else { onOff[position] = true; notificationSwitch.setChecked(true); – AndroidDev123 Jun 15 '20 at 10:52
  • I've added your code to the adapter instead of the onclick which stops the null pointers, however its still allowing me to have both switches on at the same time. – AndroidDev123 Jun 15 '20 at 11:22
  • 1
    have you called notifyDatasetChanged() method after this? – Shubham Raitka Jun 15 '20 at 15:04
  • I havent, but I have added that now. The problem I am facing is that I cannot set the switch to On/Off in the OnItemClickListener in the main activity as the switch is located in the adapter layout.xml file not the layout file of the main activity. Therefore when I try to set the switch to on I get a null pointer. I don't know how to access the switch in the main activity without getting a null pointer. – AndroidDev123 Jun 15 '20 at 15:24
  • 1
    check edited answer, and let know if you're still facing any issue. – Shubham Raitka Jun 15 '20 at 16:12
  • Its 90% working after your updated the answer. When I click switch1 it turns on, when I click switch 2 it turns on and switch 1 turns off - this is what I wanted. However if I click switch 1 again both will stay on until I click a second time then they both switch off. – AndroidDev123 Jun 15 '20 at 17:19
  • The problem is with the for loop, if the position is 0 it doesnt switch off switch 2 which is position 1 – AndroidDev123 Jun 15 '20 at 17:36
  • Sorry, by mistake I had used break instead of continue. Now, it'll work surely. – Shubham Raitka Jun 17 '20 at 11:01