2012-01-04 25 views
0

我有这样的:萨姆字符串和整数

例如:

codincidenceSelected.id - >具有值 “1”

INT一个 - >具有值 “1”

当我总结这个整数和字符串它给我“发生率:11”,但我想“发生率:2”,所以我的代码它不是总结,它是加上整数的值。

这里是我的代码:

CodIncidence codIncidenceSelected = new CodIncidence(); 
     app = (netAppApplication)getApplicationContext(); 
     codIncidenceSelected = app.getcodincidenceActual(); 

     int one = 1; 

     String total = codIncidenceSelected.id + one; 


     Toast toast=Toast.makeText(this, "Incidence: " + total, 5000); 
     toast.show(); 
+0

你的id是一个字符串,你应该把它转换为一个整数:Integer.parseInt(codIncidenceSelected.id); – 2012-01-04 12:33:37

+0

这是与Java相关的非常基本的问题,而不是android。在学习Java编程概念之后开始开发应用程序是很好的。 – 2012-01-04 12:40:39

回答

1
int total = Integer.parseInt(codIncidenceSelected.id) + one; 
+0

它的作品!感谢所有人的回应。 – HaOx 2012-01-04 12:50:04

0

请更改以下行

String total = codIncidenceSelected.id + one; 

int total = Integer.parseInt(codIncidenceSelected.id) + one; 

,然后运行项目。

+0

您需要将总数转换为烤面包的字符串 – Flexo 2012-01-04 12:35:17

+0

您错了,因为'Integer.parseInt(codIncidenceSelected.id)+ one;'返回一个整数,而不是一个String。看到我的答案。 – clemp6r 2012-01-04 12:35:45

+0

第二行是更新代码行。 – 2012-01-04 12:37:32

1

试试这个

String total = String.valueOf(Integer.parseInt(codIncidenceSelected.id) + one); 
0

对不起,我不是一个Android开发人员,但我认为,首先,你需要确保,即totalint,不string,然后,变换codIncidenceSelected.idint

UPD:哦,当我在写你已经得到了答案如何做到这一点)

0

这是因为Java是解释你的+算子a是一个字符串连接。

如果要总结的值,那么我建议你做这样的事情:

int total = codIncidenceSelected.id + one; 

Toast toast=Toast.makeText(this, "Incidence: " + Integer.toString(total);, 5000); 
toast.show(); 

当然这是假设codIncidenceSelected.id是Java的int。如果是字符串,则需要执行以下操作:

int id = Integer.parseInt(codIncidenceSelected.id); 

求和之前。

如果你真的只需要加上数字1到ID,然后我也建议你做一些这样的:

int total = codIncidenceSelected.id++; 

再次,假设codIncidenceSelected.id是一个int(否则转换首先作为为int以上)。

1

这应该工作。您的id的价值是String。您必须首先将其解析为Integer才能执行计算。然后将String的值解析回Integer

int total = Integer.parseInt(codIncidenceSelected.id) + one; 
    String answer = String.valueOf(total); 

    Toast toast=Toast.makeText(this, "Incidence: " + answer, 5000); 
     toast.show(); 
+0

嗨@尼塔...... – Android 2012-01-17 12:36:07

0

您应该先将codIncidenceSelected转换为int,然后对它们进行求和。试试这个方法

Integer.parseInt(codIncidenceSelected).