按照此处找到的说明,我正在尝试将字符串数组转换为字符串,但是我得到了一些无法删除的空值。
我一直在使用的代码:
首先,我填写 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 行,每个齿轮一个。
当我发送带有结果的电子邮件时,这就是我得到的:
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 * 空
我很迷茫,你能帮我一把吗?
谢谢!