【问题标题】:Display filename readline with switch case android使用switch case android显示文件名readline
【发布时间】:2013-10-28 16:03:43
【问题描述】:

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

(对于低于 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
    }

}

【问题讨论】:

    标签: android text switch-statement filenames readline


    【解决方案1】:

    在 7 下的 Java 版本中如何不能在字符串上使用 switch/case,考虑使用枚举,但是你的 switch 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      相关资源
      最近更新 更多