0

I have an issue with if else condition, I have fragments when I choose some options (radioButtons) and then they are pushed to activity to give a result. I'm trying to get some methods like by polymorphism but I still can't get the same result as with the if else statement. SharedPreferences could be replaced with something in this case to get the results from another fragment or not? Thanks for help.


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


    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    String size = sharedPref.getString("size", "");
    String activity = sharedPref.getString("activity", "");
    String children = sharedPref.getString("children", "");
    String dog = sharedPref.getString("dog", "");
    TextView dogName = findViewById(R.id.dogName);
    TextView dogName2 = findViewById(R.id.dogName2);

    dogName.setText(dog);
    dogName2.setText(dog);

    ImageSwitcher switchImageView = findViewById(R.id.imageSwitcher);

    ImageView imageView2 = findViewById(R.id.imageView2);
    imageView2.setImageDrawable(null);

    switchImageView.setFactory(() -> {
      ImageView imageView = new ImageView(getApplicationContext());
      if (size.equalsIgnoreCase("small") &&
          activity.equalsIgnoreCase("lazy") &&
          children.equalsIgnoreCase("like")) {
        imageView.setImageResource(R.drawable.bulldog);
        dogName.setText("Bulldog");
      }else if (
          size.equalsIgnoreCase("miniature") &&
              activity.equalsIgnoreCase("light active") &&
              children.equalsIgnoreCase("like")
      ) {
        imageView.setImageResource(R.drawable.chihuahua);
        dogName.setText("Chihuahua");
        imageView2.setImageResource(R.drawable.york);
        dogName2.setText("York");
      } else if (
          size.equalsIgnoreCase("miniature") &&
              activity.equalsIgnoreCase("light active") &&
              children.equalsIgnoreCase("no matter")
      ) {
        imageView.setImageResource(R.drawable.york);
        dogName.setText("York");
      }

The if statement goes longer but i just wanted to show the method.

Andy
  • 3
  • 1
  • 2
  • It is pretty unclear to me which issue you have with if else condition. The only thing i i see is that it is unreadable as i have to scroll a lot before i know if there is code after the if else blocks. – blackapps Dec 03 '20 at 07:52
  • If there is no further code after the if else blocks then do not use else. Just return if the first if comes true. After that the next if and return. Makes it readable. No need to scroll down. – blackapps Dec 03 '20 at 07:56
  • I just copied 3 of the if else statement, but i last for 20 more examples. I just want to know if there is a better method to show this, because its not looking like clean code at the moment. – Andy Dec 03 '20 at 08:38
  • You are not reacting to what i said. – blackapps Dec 03 '20 at 09:35
  • The issue is that if-else statement is not looking good to read it. Ok,I can erase else from statement but i would like to make this code more readable. There is a return imageView at the end of it btw. But my question is, is there any method to combine this kind of statement with fragments with radioButtons. – Andy Dec 03 '20 at 09:58
  • Sorry, i have no idea what you want. – blackapps Dec 03 '20 at 10:33
  • I want to replace if method with another method like switch/or something like polymorphism. I don't know how to explain it in other words. – Andy Dec 04 '20 at 07:25

0 Answers0