0

I am implementing an Android app in which I have a list view. Each element of this list view has a toggle button and, using a custom adapter, which extends BaseAdapter, I managed to get which toggle buttons are checked and which are not checked. But, what if I wanted to impose constraints to these toggle buttons? Suppose that when I check the second toggle button, I want that the first one is automatically checked too. How should I do? Thanks

       public class MyCustomAdapter<T> extends BaseAdapter {
Context mContext; //the application context
LayoutInflater mInflater; //object that is used to set the layout of the View
ArrayList<T> mList; //list of the items shown in the user interface
SparseBooleanArray mSparseBooleanArray; //object that is used to keep track of the selected items of the list

ToggleButton[] toggleButtons;

public MyCustomAdapter(Context context, ArrayList<T> list) {

    this.mContext = context;
    mInflater = LayoutInflater.from(mContext);
    mSparseBooleanArray = new SparseBooleanArray();
    mList = new ArrayList<T>();
    this.mList = list;
    toggleButtons = new ToggleButton[list.size()];
}
public View getView (int position, View convertView, ViewGroup parent) {

    if(convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_with_toggle, null);
    }

    toggleButtons[position] = (ToggleButton)convertView.findViewById(R.id.toggleEnable);


    switch (position){

        case 0:
            toggleButtons[position].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Toast.makeText(mContext, ""+0, Toast.LENGTH_SHORT).show();
                }
            });
            break;
        case 1:                
            toggleButtons[position].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //Toast.makeText(mContext, ""+isChecked, Toast.LENGTH_SHORT).show();
                    if(isChecked) {
                        if(!toggleButtons[0].isChecked()) {
                           // Toast.makeText(mContext, "elemento 1 "+toggleButtons[1].isChecked(), Toast.LENGTH_SHORT).show();
                            toggleButtons[0].setChecked(true);


                        } else {
                            //things to do if toggleButtons[0] == true
                        }
                    } else {
                      //things to do if isChecked == false
                    }

                }
            });
            break;
        default:
              break;
    }

    return convertView;
}
}
rosekarma
  • 117
  • 1
  • 1
  • 12

1 Answers1

1

Presumably you have an object of some sort which represents each row in the list, if so you can try something like this:

  • Add a list/array of linked items to your object and populate it where applicable.
  • On button click, update the linked items.
  • Call notifyDataSetChanged() and force the list to be redrawn.
Philio
  • 3,675
  • 1
  • 23
  • 33
  • Sorry, I still don't have a clue of how to do. Be patient, please. What can I do in order to set the first toggle button checked as soon as I set the second toggle button checked? – rosekarma Oct 27 '14 at 17:12