字符串的字符串数组:使用Implode的问题

时间:2013-04-05 11:45:31

标签: android arrays string implode

按照找到here的指示,我正在尝试将字符串数组转换为字符串,但我得到一些我无法删除的 nulls

我一直在使用的代码:

首先,我填写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行,每个齿轮一个。

Results from 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 * null
  3,38 * 3,94 * 4,50 * 4,98 * 5,56 * 6,30 * 6,75 * 7,27 * 7,88 * 8,59 * null

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

谢谢!

2 个答案:

答案 0 :(得分:0)

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

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

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

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

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

答案 1 :(得分: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);

        }