user12345
user12345

Reputation: 55

Why is my else statement not working for Cardview with two functions?

I have a CardView that works as a Start/Stop function.

Start, when pressed, works. However Stop will not work. I know that the onClick event is working as the text view changes, however the actual function does not work. Any help greatly appreciated. I will post whole code below.

 //Defining Cards on Landing Page
    appsCard = (CardView) findViewById(R.id.apps_card);
    parentalControlsCard = (CardView) findViewById(R.id.parentalControls_id);
    customSettingsCard = (CardView) findViewById(R.id.customSettings);
    activateCard = (CardView) findViewById(R.id.activate_id);
    StartStopCard = (CardView) findViewById(R.id.StartStopCard);


    //Add OnClick Listeners
    appsCard.setOnClickListener(this);
    parentalControlsCard.setOnClickListener(this);
    customSettingsCard.setOnClickListener(this);
    activateCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ruleSets.isEmpty()) {
                Toast.makeText(LandingPage.this, "You did not create a custom setting.", Toast.LENGTH_SHORT).show();
            } else {
                PendingIntent pending_start;
                PendingIntent pending_stop;
                Intent startIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
                Intent stopIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
                Calendar startTime = new GregorianCalendar();
                Calendar endTime = new GregorianCalendar();
                String startString = ruleSets.get(0).getStartTime();
                String endString = ruleSets.get(0).getEndTime();

                String[] startArr = startString.split(":");
                String[] endArr = endString.split(":");

                startTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startArr[0]));
                startTime.set(Calendar.MINUTE, Integer.parseInt(startArr[1]));

                endTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endArr[0]));
                endTime.set(Calendar.MINUTE, Integer.parseInt(endArr[1]));

                pending_start = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                pending_stop = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                startIntent.putExtra("status", "start");
                stopIntent.putExtra("status", "stop");

                Toast.makeText(LandingPage.this, "Your ruleset will start at " + startString, Toast.LENGTH_SHORT).show();

                setStatus("Lock Active");
                setProcess("Stop");

                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_start);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_stop);
            }
        }
    });


    CardView StartStopCard = (CardView) findViewById(R.id.StartStopCard);
    StartStopCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        mStarted = !mStarted;
            if (mStarted) {
                mStarted=false;
                SharedPreferences setting = getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = setting.edit();
                editor.remove("switcher");
                editor.remove("switcher2");
                editor.remove("lockStatus");
                editor.remove("processStatus");
                editor.commit();
                editor.putString("switcher", "true");
                editor.putString("switcher2", "true");
                editor.putString("lockStatus", "Lock Active");
                editor.putString("processStatus", "Start");
                editor.apply();
                intent.putExtra("status", "start");
                sendBroadcast(intent);
                intent.putExtra("processStatus", "start");
                sendBroadcast(intent);
                String status = setting.getString("lockStatus", "");
                setStatus(status);
                String processStatus = setting.getString("processStatus" , "");
                setProcess(processStatus);

            }
            else {
                mStarted= true;
                SharedPreferences setting = getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = setting.edit();
                editor.remove("switcher");
                editor.remove("switcher2");
                editor.remove("lockStatus");
                editor.remove("processStatus");
                editor.commit();
                editor.putString("switcher", "false");
                editor.putString("switcher2", "false");
                editor.putString("lockStatus", "Lock Deactivated");
                editor.putString("processStatus", "Stop");
                editor.apply();
                intent.putExtra("status", "stop");
                sendBroadcast(intent);
                intent.putExtra("processStatus", "start");
                sendBroadcast(intent);
                String status = setting.getString("lockStatus", "");
                setStatus(status);
                String processStatus = setting.getString("processStatus" , "");
                setProcess(processStatus);

            }
        }

    });

}

...............................................................................

Upvotes: 1

Views: 89

Answers (1)

Levi Moreira
Levi Moreira

Reputation: 12005

The problem is in here

 mStarted = !mStarted;
        if (mStarted) {
            mStarted=false;
            ...
        else{
            mStarted = true;
        ...
        }

Let's say your mStarted starts in true. It is reverted before the if statement and then turned to true again, because it goes to else. When you press the button it will do the same thing, it will get reverted and go to the same else. The same if mStarted is false from the begining. The point is, it will always go to the same branch.

If you want to toggle the behaviour on clicks (each click goes to a different branch of the if statement) then you either remove mStarted = !mStarted; or remove the mStarted assignements from the branches.

Upvotes: 1

Related Questions