简体   繁体   中英

How do I display my TextView using boolean with a if-else statement in Android?

So I have a TextView named displayAnswer; I set it to false on my setOnClickListener button function. My if else statement is supposed to check if what the User entered is Palindrome. If it is it should display the text, but for some reason, it says its assigned but never accessed. Still new to Android Studio any feedback positive or negative will help. I also left the Java code in comments on what is suppose to be printed.

public class MainActivity extends AppCompatActivity {

EditText userEntry;
Button checkPalindrome;
TextView displayAnswer;


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

    userEntry = findViewById(R.id.edit_demo);
    checkPalindrome = findViewById(R.id.button_demo);
    displayAnswer = findViewById(R.id.guess);

    checkPalindrome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String userEntry, reverse = ""; // Objects of String class
            boolean displayAnswer = false;
            Scanner in = new Scanner(System.in);

   //System.out.println("Enter a string to check if it is a palindrome");

            userEntry = in.nextLine();
            int length = userEntry.length();

            for (int i = length - 1; i >= 0; i--)
                reverse = reverse + userEntry.charAt(i);

            if (userEntry.equals(reverse)){
                displayAnswer = true;}
                //System.out.println("The string is a palindrome.");
            else{
                displayAnswer = false;}
                //System.out.println("The string isn't a palindrome.");

        }
    });
}
}

try this ,

public class MainActivity extends AppCompatActivity {

EditText userEntry;
Button checkPalindrome;
TextView displayAnswer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userEntry = findViewById(R.id.edit_demo);
    checkPalindrome = findViewById(R.id.button_demo);
    displayAnswer = findViewById(R.id.guess);

    checkPalindrome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String reverse,stringUserEntry = ""; // Objects of String class
            boolean displayAnswerBool = false;
            stringUserEntry= userEntry.getText().toString();
            reverse=new StringBuilder(stringUserEntry).reverse().toString();

            if (stringUserEntry.equals(reverse)){
                displayAnswerBool = true;
                displayAnswer.setText("this is a palindrome");
            }
            //System.out.println("The string is a palindrome.");
            else{
                displayAnswerBool = false;
                displayAnswer.setText("this is not a palindrome");}
            //System.out.println("The string isn't a palindrome.");

        }
    });

}
}

I think this is what you want.

You are shadowing the outer TextView displayAnswer with a local boolean of the same name.

You will need to reference MainActivity.this.displayAnswer within the ClickListener


You can also move the listener to the outer scope by implementing the interface on the class

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView displayAnswer;

    // ... onCreate() {
        // checkPalindrome.setOnClickListener(this);
    // }

    @Override 
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guess:
                String entry = MainActivity.this.userEntry.getText().toString();
                String reverse = new StringBuilder(entry).reverse().toString();
                if (entry.equals(reverse)) {
                    MainActivity.this.displayAnswer.setText("yes");
                } else {
                    MainActivity.this.displayAnswer.setText("no");
                }
        }
    }


}

You may also try Data binding to dynamically switch the view based on the boolean value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM