0

按照此处找到的说明,我正在尝试将字符串数组转换为字符串,但是我得到了一些无法删除的空值。

我一直在使用的代码:

首先,我填写 textviews 并用结果制作数组。

public void doCalculations(){
    //write number of teeth for every cog from a cassette
    for(int i=0; i<cassCogs[0]; i++){
        tvCogsArray[i].setBackgroundColor(Color.LTGRAY);
        tvCogsArray[i].setText(String.valueOf(cassCogs[i+1]) +" " + getString(R.string.teeth));
        }

    if (Integer.parseInt(chainringCount) == 1){
        for (int counter = 0; counter<cassCogs[0]; counter++ ){
            float temp = (Float.parseFloat(wheelsize) *     (Float.parseFloat(chainringOne) /cassCogs[counter + 1] ))/1000;
            tvOne[counter].setText(String.format("%.2f", temp));

            toEmail_1[counter] = String.format("%.2f", temp);
        }
    }else if (Integer.parseInt(chainringCount) == 2){
        for (int counter = 0; counter<cassCogs[0]; counter++ ){
            float temp = (Float.parseFloat(wheelsize) * (Float.parseFloat(chainringOne)/cassCogs[counter + 1] ))/1000;
            tvOne[counter].setText(String.format("%.2f", temp));

            toEmail_1[counter] = String.format("%.2f", temp);

            float second = (Float.parseFloat(wheelsize) *(Float.parseFloat(chainringTwo)/cassCogs[counter+ 1] ))/1000;
            tvTwo[counter].setText(String.format("%.2f", second));

            toEmail_2[counter] = String.format("%.2f", second);

        }
    }else if (Integer.parseInt(chainringCount)==3){
        for (int counter = 0; counter<cassCogs[0]; counter++ ){
            float temp = (Float.parseFloat(wheelsize) *(Float.parseFloat(chainringOne)/cassCogs[counter + 1] ))/1000;
            tvOne[counter].setText(String.format("%.2f", temp));

            toEmail_1[counter] = String.format("%.2f", temp);


            float second = (Float.parseFloat(wheelsize) *(Float.parseFloat(chainringTwo)/cassCogs[counter + 1] ))/1000;
            tvTwo[counter].setText(String.format("%.2f", second));

            toEmail_2[counter] = String.format("%.2f", second);


            float third = (Float.parseFloat(wheelsize)*(Float.parseFloat(chainringThree)/cassCogs[counter+ 1] ))/1000;
            tvThree[counter].setText(String.format("%.2f", third));

            toEmail_3[counter] = String.format("%.2f", third);

            }
             }
       }

通过电子邮件发送结果的功能

      private void mail(){

    Intent i = new Intent (Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{});
    i.putExtra(Intent.EXTRA_SUBJECT, getApplicationContext().getString(R.string.calculos));
    i.putExtra(Intent.EXTRA_TEXT, composeBody());

    try{
        startActivity(Intent.createChooser(i, "Send Mail..."));
    }catch (android.content.ActivityNotFoundException ex){
     Toast.makeText(getApplicationContext(), "There are no...", Toast.LENGTH_LONG).show();
    }

}

在这里,我写了电子邮件正文:

public String composeBody(){
    String bodyText="";
    String join1="";
    String join2="";
    String join3="";
    if(Integer.parseInt(chainringCount)==1){
        if (toEmail_1.length >0) {
            join1 = implodeArray(toEmail_1, glueString);
        }
        bodyText= bannerText  + lineSep + join1;
    }else if (Integer.parseInt(chainringCount)==2){

        if (toEmail_1.length >0) {
            join1 = implodeArray(toEmail_1, glueString);
        }
        if (toEmail_2.length >0){
            join2=implodeArray(toEmail_2, glueString);
        }
        bodyText= bannerText  + lineSep + join1 + lineSep + join2;
    }else if(Integer.parseInt(chainringCount)==3){
        if (toEmail_1.length >0) {
            join1 = implodeArray(toEmail_1, glueString);
        }
        if (toEmail_2.length >0){
            join2=implodeArray(toEmail_2, glueString);
        }
        if(toEmail_3.length>0) {
            join3=implodeArray(toEmail_3,glueString);
        }
        bodyText= bannerText  + lineSep + join1 + lineSep + join2 + lineSep +join3;         
    }       
    return bodyText;

将字符串数组转换为字符串:

 public static String implodeArray(String[] inputArray, String glueString){
String output= "";
if(inputArray.length > 0){
    StringBuilder sb = new StringBuilder();
    sb.append(inputArray[0]);

    for (int i=1; i<inputArray.length;i++){
        sb.append(glueString);
        sb.append(inputArray[i]);
    }
    output=sb.toString();
}
return output;
 }

在我的手机屏幕上,我可以看到以下内容:2 列,每个链环一个 10 行,每个齿轮一个。

doCalculations 的结果

当我发送带有结果的电子邮件时,这就是我得到的:

ULTEGRA 11-28 * 36/46 2,64 * 3,08 * 3,52 * 3,89 * 4,35 * 4,93 * 5,28 * 5,69 * 6,16 * 6,73 * 空
3 ,38 * 3,94 * 4,50 * 4,98 * 5,56 * 6,30 * 6,75 * 7,27 * 7,88 * 8,59 * 空

我很迷茫,你能帮我一把吗?

谢谢!

4

2 回答 2

0

好吧,这很容易,感谢@ssantos的帮助:

我最初的数组声明考虑到了最大容量

String[] toEmail_1 = new String[11];
String[] toEmail_2 = new String[11];
String[] toEmail_3 = new String[11];

现在,我为要使用的数组设置容量:

   ...else if (Integer.parseInt(chainringCount) == 2){
        toEmail_1 = new String[cassCogs[0]];
        toEmail_2 = new String[cassCogs[0]];

        for (int counter = 0; counter<cassCogs[0]; counter++ ){
            float temp = (Float.parseFloat(wheelsize) * (Float.parseFloat(chainringOne) /cassCogs[counter + 1] ))/1000;
            tvOne[counter].setText(String.format("%.2f", temp));

            toEmail_1[counter] = String.format("%.2f", temp);

            float second = (Float.parseFloat(wheelsize) *(Float.parseFloat(chainringTwo) /cassCogs[counter+ 1] ))/1000;
            tvTwo[counter].setText(String.format("%.2f", second));

            toEmail_2[counter] = String.format("%.2f", second);

        }
于 2013-04-07T22:14:11.273 回答
0

您的 implodeArray 函数似乎没问题,很可能您将这些空值放在上面的代码中。两个快速测试。-

  • 尝试调试/记录要添加到数组中的值,例如

    toEmail_1[counter] = String.format("%.2f", temp);

  • 只是为了确保 implodeArray 正常工作,请尝试使用模拟数组调用它

    implodeArray(new String[]{"foo1","foo2","foo3","foo4","foo5","foo6"}, "*")

于 2013-04-05T12:47:17.570 回答