繁体   English   中英

用开关盒android显示文件名readline

[英]Display filename readline with switch case android

我想知道为什么我的编码的这一部分不起作用,并且程序在切换时总是给我同样的错误。

(无法为低于1.7的源级别打开String类型的值。仅允许使用可转换的int值或枚举变量)

我想读取保存为txt文件的文件,并将其显示在程序上。

例如:我在这里写了“电子邮件”作为案例,所以我写了我想要的内容并将其保存到txt文件中,以便在此开关中读取。

谁能帮我解决这个问题? 深表感谢。 谢谢。

这是我的代码:

        private void ExecuteCommands(String filename) {
     //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard, filename + ".txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            String[] tmp = line.split(" ");

                        //this switch case giving me problem
                      switch(tmp[0]){

             case "Email":
                String subject = sbj.getText().toString();
                String message = messageBody.getText().toString();
                String to = destinationAddress.getText().toString();

                Intent emailActivity = new Intent(Intent.ACTION_SEND);

                //set up the recipient address
                emailActivity.putExtra(Intent.EXTRA_EMAIL, new String[] { to });

                //set up the email subject
                emailActivity.putExtra(Intent.EXTRA_SUBJECT, subject);

                //you can specify cc addresses as well
                // email.putExtra(Intent.EXTRA_CC, new String[]{ ...});
                // email.putExtra(Intent.EXTRA_BCC, new String[]{ ... });

                //set up the message body
                emailActivity.putExtra(Intent.EXTRA_TEXT, message);

                emailActivity.setType("message/rfc822");

                startActivity(Intent.createChooser(emailActivity, "Select your Email Provider :"));
                break;

            case "SMS message":
             String phoneNo = textPhoneNo.getText().toString();
              String sms = textSMS.getText().toString();

              try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
              } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                    "SMS faild, please try again later!",
                    Toast.LENGTH_LONG).show();
                e.printStackTrace();


                break;
              }
            }
        }


    }



        catch (IOException e) {
        //You'll need to add proper error handling here
    }

}

如何在7以下的Java版本中不能在字符串上使用switch / case,请考虑使用enum,但是您的case case字符串如何包含空白,您无法通过valutOf枚举方法来检索枚举常量,但是可以添加自己的方法基于特定字符串检索对应的枚举。 像这样。

enum Type {

EMAIL {
    @Override
    public boolean counterpart(String value) {
        if (value.equals(EMAIL)) {
            return true;
        }
        return false;
    }
},
SMS {
    @Override
    public boolean counterpart(String value) {
        if (value.equals(SMS_TAG)) {
            return true;
        }
        return false;
    }
};
private static final String EMAIL_TAG = "Email";
private static final String SMS_TAG = "SMS Message";

public abstract boolean counterpart(String value);

}

一个公共静态方法,该方法可以根据字符串值返回对应的类型。

 public static Type  getType( String value ) {
    for (Type t : Type.values()) {
        if (t.counterpart(value )) {
            return t;
        }
    }
    return Type.EMAIL;
}  

那你应该像这样

    Type type = getType( param[ 0 ] );

    switch( type ){
        case EMAIL:
            break;
        case SMS:
            break;
        default:
            break;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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