简体   繁体   中英

Do you know a better way to write this method?

I created this method to handle 2 different way of creating alert dialog, depending on internet status. Do you know a better way to get the same result? Using .equals() on strings in if-else block do not seem a best-practices way... Am i right?

public void noInternetAlertDialog(String errorMsg) {
    String title = null;
    String msg = null;

    if (errorMsg.equals("none")) {
        title = "Connection failded";
        msg = "Please check internet connection";
    } else if (errorMsg.equals("slow")) {
        title = "Connection timeout";
        msg = "Connection is slow";
    }

    AlertDialog.Builder builder;
    builder = new AlertDialog.Builder(Main.this);
    builder.setCancelable(false);
    builder.setTitle(title);
    builder.setMessage(msg);

    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            dialog.dismiss();
            downloadDialog();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            finish();
        }
    });         
    AlertDialog dialog = builder.create();
    dialog.show();
}

It really is not a good idea to identify a state/result with a string! you should use an enum instead.

enum NoInternetResult {
    slow, none
}

and then:

public void noInternetAlertDialog(NoInternetResult result) {
String title = "Connection failded";
String msg = "Please check internet connection";

if (result==NoInternetResult.slow) {
    title = "Connection timeout";
    msg = "Connection is slow";
}

btw. use strings.xml for you strings like "retry" and "Cancel" ( http://developer.android.com/guide/topics/resources/string-resource.html )

  1. Use strings.xml for your strings to allow localization (Retry, Cancel, "Connection failded", "Please check internet connection", "Connection timeout", "Connection is slow")
  2. If your values represent something create a data type for them. I mean: if your string will report if internet is available or slow, why keep it as String? A String can be everything and convert to something which says directly what values it can assume will improve your code a lot.

     public enum InternetStatus { Offline, Slow } 

    And a == will be faster than a equals call. If you don't want to use the enum , consider using "none".equals(errorMessage)

     String title = "Connection failded"; String msg = "Please check internet connection"; if ("slow".equals(errorMsg)) { title = "Connection timeout"; msg = "Connection is slow"; } 
  3. You can chain calls to the builder and remove the variable dialog because you can call show() directly (If you still need the reference to the AlertDialog , show() still returns it).

  4. You can go with your fantasy and do something like this

     .setTitle(errorMsg == InternetStatus.Slow ? "Connection timeout" : "Please check internet connection") .setMessage(errorMsg == InternetStatus.Slow ? "Connection failded" : "Connection is slow") 

    but it will make your code a mess if you want to add more InternetStatus . You could create a method inside InternetStatus which returns the message (if it will be needed in other places too). But it highly depends on the project you are working with. You could an "extension" method which does it for you just where you need it without put it in the enum code ( enum s can have methods). You should consider every opportunity.


Maybe?

public enum InternetStatus {
  Offline,
  Slow
}

public void noInternetAlertDialog(InternetStatus errorMsg) {
  String title = getString(R.string.internetfailed);
  String msg = getString(R.string.checkyourinternet);

  if (errorMsg == InternetStatus.Slow) {
    title = getString(R.string.connectiontimeout);
    msg = getString(R.string.slowinternet);
  }

  new AlertDialog.Builder(Main.this)
      .setCancelable(false)
      .setTitle(title)
      .setMessage(msg)
      .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
          downloadDialog();
        }
      })
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
          finish();
        }
      })
      .show();
}

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