Nicholas Muir
Nicholas Muir

Reputation: 3114

Java: Set multiple variables through one argument of a function

I have 7 check boxes for each day of the week declared like this:

CheckView MondayTextBox;
CheckView TuesdayTextBox;
CheckView WednesdayTextBox;
...

And then in oncreate they are initialized like this:

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_add_alarm, container, false);

            MondayTextBox = (CheckView) view.findViewById(R.id.mondayButton);
            TuesdayTextBox = (CheckView) view.findViewById(R.id.tuesdayButton);
            WednesdayTextBox = (CheckView) view.findViewById(R.id.wednesdayButton);
            ...

In the onClick listener for each of these checkboxes I set the alpha of the clicked check box with an animation to grey out indicating it is not used like this:

            MondayTextBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectAndAnimateTheCheckBoxes(mondaySelected, MondayTextBox);
            }
        });

        TuesdayTextBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectAndAnimateTheCheckBoxes(tuesdaySelected, TuesdayTextBox);
            }
        });
        ...

Which calls selectAndAnimateTheCheckBoxes function:

public void selectAndAnimateTheCheckBoxes(Boolean daySelected, CheckView dayTextBox) {
    if (!daySelected) {
        daySelected = true;
        dayTextBox.animate().alpha(1F).setDuration(300);
    } else {
        daySelected = false;
        dayTextBox.animate().alpha(0.3F).setDuration(300);
    }
}

Is it possible to pass not just the value of a variable but the variable itself through a function? Or does the variable have to be explicitly stated and set by the argument of the function.

P.s. I know how to make this function work, I had never even thought of it before and I am wondering if is possible.

Thanks in advance for your help.

Upvotes: 1

Views: 137

Answers (3)

Ayan
Ayan

Reputation: 8906

This is probably what you are looking for:

CheckView chk[];
Boolean daySelected[];

chk=new CheckView [7];
daySelected=new daySelected[7];

chk[0]=(CheckView) view.findViewById(R.id.mondayButton);
chk[1]=(CheckView) view.findViewById(R.id.tuesdayButton);
...


for (int i = 0; i < 7; i++) {
    chk[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectAndAnimateTheCheckBoxes(daySelected[i], chk[i]);
        }
    });
}

Upvotes: 0

Ayan
Ayan

Reputation: 8906

You can do the following :

public class MainActivity extends Activity implements OnClickListener{

CheckView MondayTextBox;
CheckView TuesdayTextBox;
CheckView WednesdayTextBox;
...

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_add_alarm, container, false);

       MondayTextBox = (CheckView) view.findViewById(R.id.mondayButton);
       TuesdayTextBox = (CheckView) view.findViewById(R.id.tuesdayButton);
       WednesdayTextBox = (CheckView) view.findViewById(R.id.wednesdayButton);

       ...
       MondayTextBox.setOnClickListener(this);
       TuesdayTextBox.setOnClickListener(this);
       WednesdayTextBox.setOnClickListener(this);
       ...
  }

  @Override
  public void onClick(View v) {

  switch (v) {

   case MondayTextBox :
   selectAndAnimateTheCheckBoxes(mondaySelected, MondayTextBox);
   break;

   case TuesdayTextBox:
   selectAndAnimateTheCheckBoxes(tuesdaySelected, TuesdayTextBox);
   break;
   ...

   }

  }
}

Upvotes: 0

Rahul Khurana
Rahul Khurana

Reputation: 8844

You can setOnCheckedChangeListener to your checkboxes like this

MondayTextBox.setOnCheckedChangeListener(this);
.
.

and override the method

@Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    switch (buttonView.getId()){
        case R.id.mondayButton:

            if(isChecked == true) {
                Toast.makeText(this, "Checked", Toast.LENGTH_SHORT).show();
            } else{
                    Toast.makeText(this, "Unchecked", Toast.LENGTH_SHORT).show();
                   }

            break;
         }

      }

Upvotes: 2

Related Questions