mardi 16 janvier 2018

checkBox if else statement in android app is not wroking

The intermediate stage of my app

My problem is that when I check the "Coffee" in app and press "Order" button. The "selectionDetail" area shows "Ice Cream Selected" but it should show "Coffee is selected"

More simply to say maybe I am messing with if else statement. "selectIceCream" should remain flase since I haven't check the box but it becomes automatically true. please check the image to understand the problem. the main problem

Please help me, what is the mistake I haven't care? The following is my javacode.

package com.infinity.maruf.scroll;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    int n = 0;
    boolean selectIceCream = false;
    boolean selectCoffee = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void increase(View view) {
        n = n + 1;
        if(n>15){n=15;} //maximum item number is 15
        displayItemNumber();
    }

    private void displayItemNumber() {
        TextView orderNumber = findViewById(R.id.itemNumber);
        orderNumber.setText("" + n);
    }


    public void decrease(View view) {
        n = n - 1;
        if(n<0){n=0;} //minimum item number is 0
        displayItemNumber();
    }

    public void submitOrder(View view) {
        displayOrderDetails();

        iceCream();
}

    private void displayOrderDetails() {
        TextView orderDetails = findViewById(R.id.orderDetail);
        orderDetails.setText("Mohammad Maruf Uddin");
    }

    private void iceCream() {
        CheckBox checkBoxIceCream = (CheckBox) findViewById(R.id.checkBox1);
        selectIceCream = checkBoxIceCream.isChecked();

        CheckBox checkBoxCoffee = (CheckBox) findViewById(R.id.checkBox2);
        selectCoffee = checkBoxCoffee.isChecked();
        displayItem();

    }


    private void displayItem( ) {
        TextView selectedItem = findViewById(R.id.selectionDetail);
        if(selectCoffee=true){
        selectedItem.setText("Coffee Selected");
            Log.v( "MainActivity", "selectCoffee=true");
            ImageView selectImage = findViewById(R.id.imageView);
            selectImage.setImageResource(R.drawable.coffee);

    }
    else{Log.v( "MainActivity", "Coffee not selected");
            selectedItem.setText("");}
    if (selectIceCream=true){
        selectedItem.setText("Ice Cream Selected");
        Log.v( "MainActivity", "selectIceCream=true");
        ImageView selectImage = findViewById(R.id.imageView);
        selectImage.setImageResource(R.drawable.coffee);

        }
    else {Log.v( "MainActivity", "Ice Cream not selected");
        selectedItem.setText("");}
    }


}




Aucun commentaire:

Enregistrer un commentaire