android getString为int

时间:2014-12-29 16:17:45

标签: android spinner

我从Web服务获取数据,这个数据在spinner对象上显示并且工作正常...所以当我选择一个Spinner时,我无法将值“ultima_jornada”(其String值)转换为Integer。你可以帮我吗?

public void onItemSelected(AdapterView<?> adapter, View view, int position,
        long id) {
    String item = adapter.getItemAtPosition(position).toString();
    Toast.makeText(getApplicationContext(),item,
               Toast.LENGTH_LONG).show();
      JSONObject json_obj;
    try {
        json_obj = ljsonArray.getJSONObject(position);
        String lsJornada = json_obj.getString("ultima_jornada");
        int jornada = Integer.parseInt(lsJornada);
        jornada = jornada;
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

Web服务的数据

{"Datos":[{"id_torneo":"9","tor_nombre":"10:00 P.M","ultima_jornada":"0"},{"id_torneo":"11","tor_nombre":"7:00 PM","ultima_jornada":"15"},{"id_torneo":"7","tor_nombre":"8:00 PM","ultima_jornada":"4"},{"id_torneo":"8","tor_nombre":"9:00 PM","ultima_jornada":"2"},{"id_torneo":"5","tor_nombre":"Atardecer Ant.","ultima_jornada":"17"},{"id_torneo":"3","tor_nombre":"De los Desvelados","ultima_jornada":"16"},{"id_torneo":"12","tor_nombre":"Dominical Femenil","ultima_jornada":"0"},{"id_torneo":"13","tor_nombre":"Dominical Varonil","ultima_jornada":"0"},{"id_torneo":"10","tor_nombre":"Juvenil Sabatino","ultima_jornada":"0"},{"id_torneo":"6","tor_nombre":"Sabatino Libre","ultima_jornada":"8"}],"status":0,"mensaje":""} 

1 个答案:

答案 0 :(得分:0)

如果没有errolog,很难定义抛出异常的内容,但我认为这一行是错误的:
Integer.parseInt 它应该是 Integer.valueOf 因此抛出异常是因为你只是抛出一个Json异常......
如果我的提示不起作用,请将交换更改为基类异常,从而将在catch语句中抛出所有错误