如何从listview和JSONObject中获取信息?

最后发布: 2015-03-01 19:44:05


问题

我有一个ListView,它是由几个单元格(BaseAdapter)创建的,这些单元格填充了JSONOBject中的信息。 单击某个项目时,我想获取与单元格一起出现的信息并将其放入alertDialog中。 我怎么处理这个?

到目前为止,我正在尝试这种方式。 但我得到一个NullPointerException:

UPDATE

public class ApiConnector {

public JSONArray OpenCalls()
{

    String url = "http://IPIPIPIPIPIPIP/masterview/mobilephp/getOpenCalls.php";


    HttpEntity httpEntity = null;

    try
    {

        DefaultHttpClient httpClient = new DefaultHttpClient();  
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();


    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {

            String entityResponse = EntityUtils.toString(httpEntity);

            Log.e("Entity Response  : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;
}

OpenCallsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String clickedCallName = null;

                try {

                    JSONObject clickedPosition = jsonArray.getJSONObject(position);
                    String callNumber = clickedPosition.getString("callingworkerID").toString();

                    if (callNumber.equals("0")) {
                        clickedCallName = "Fred";

                    } else if (callNumber.equals("1")) {
                        clickedCallName = "Jona";

                    } else if (callNumber.equals("2")) {
                        clickedCallName = "Sebastian";

                    } 

                } catch (JSONException e) {
                    e.printStackTrace();
                }


                String msg = "Do you want to call" + clickedCallName + " again?";

                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AgainCalls.this);

                dialogBuilder.setTitle("Call back");
                dialogBuilder.setMessage(msg);
                dialogBuilder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                dialogBuilder.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog dialog = dialogBuilder.create();
                dialog.show();

            }});

更新logcat错误:

Process: de.denjo.master, PID: 22745
java.lang.NullPointerException
        at de.denjo.master.AgainCalls$1.onItemClick(AgainCalls.java:89)
android listview onclick baseadapter