解析多个json对象值,该值在该对象的最后一个值中。

时间:2014-04-22 03:06:13

标签: android json android-intent infowindow

我正在开发一个应用程序,我从远程服务器获得响应(Json)。我也能够根据该响应解析和放置标记。但是我无法解析来自响应的ph数据并将其传递给另一项活动。

在setOnInfoWindowClickListener事件中只发送最后一个json对象数据的电话号码

我知道我必须做一些小修改。请在此建议我。

这是我得到的Json回应。

[
{
id: 965,
distance: "1.47",
ph: "33441111",
name: "XYZ",
LS: " abcdef",
point: "77.588018,12.959282"
},
{
id: 965,
distance: "1.47",
ph: "33441111",
name: "XYZ",
LS: " abcdef",
point: "77.588018,12.959282"
},
.
.
]

我试过这种方式来解析

private class HttpGetTask extends AsyncTask<Void, Void, String> {

        // Showing progress dialog
        // Passing URL

    @Override
    protected String doInBackground(Void... params) {

        // http stuff
    }

    @Override
    protected void onPostExecute(String result) {

        if (pDialog.isShowing())
            pDialog.dismiss();
        try {
            JSONArray json = new JSONArray(result);

            for (int i = 0; i < json.length(); i++) {
                Log.v("Response", result);
                final JSONObject e = json.getJSONObject(i);
                String point = e.getString("point");

                final String phone = e.getString("ph");                 

                String[] point2 = point.split(",");
                double lat1 = Double.parseDouble(point2[0]);
                double lng1 = Double.parseDouble(point2[1]);

                gMap.addMarker(new MarkerOptions()
                        .title(e.getString("name"))
                        .snippet(
                                e.getString("LS") + "*" + e.getString("ph"))
                        .position(new LatLng(lng1, lat1))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.pmr)));

                gMap.setInfoWindowAdapter(new InfoWindowAdapter() {

                    @Override
                    public View getInfoWindow(Marker arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker mrkr) {
                        // TODO Auto-generated method stub
                        String name = mrkr.getTitle();
                        String detail = mrkr.getSnippet();
                        String trimmedDetail = detail.substring(0, 60);

                        Log.v("Info", name + " " + detail);
                        View v = getLayoutInflater().inflate(
                                R.layout.infowindow, null);
                        TextView title = (TextView) v
                                .findViewById(R.id.titleTV);
                        TextView snippet = (TextView) v
                                .findViewById(R.id.snippetTV);

                        title.setText("" + name);
                        snippet.setText("" + trimmedDetail);

                        return v;
                    }
                });

                gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker arg0) {

                        Intent myIntent = new Intent(getBaseContext(),
                                DtActivity.class);
                        myIntent.putExtra("title", arg0.getTitle());
                        myIntent.putExtra("detail", arg0.getSnippet());
                        myIntent.putExtra("ph1", phone);

// How to access and send ph here.

                        try {
                            String ph = e.getString("ph");
                            myIntent.putExtra("ph", ph);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        startActivity(myIntent);
                    }
                });

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (null != mClient)
            mClient.close();

    }
}

3 个答案:

答案 0 :(得分:3)

您需要初始化gMap Globaly并在for循环中添加标记。从表单循环中删除适配器和infowindow单击侦听器代码并将其粘贴到外部。请检查以下样本。

public class Example extends FragmentActivity implements OnInfoWindowClickListener {

public static GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);
    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    mMap.setOnInfoWindowClickListener(this);
    // Inside the Loop

    mMap.addMarker(new MarkerOptions().title(e.getString("name"))
            .snippet(e.getString("LS") + "*" + e.getString("ph"))
            .position(new LatLng(lng1, lat1))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pmr)));
    // Close the loop

}

@Override
public void onInfoWindowClick(Marker marker) {
    // here you can get title, snippet, lat,lng of selected marker. then you
    // can start activity.
}

}

请将for循环粘贴到Asyntask类的onPostExecute中。

答案 1 :(得分:1)

添加此代码段以获取电话号码。我已使用you will get your phone number here

对其进行了标记
gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker arg0) {

  ///////////////you will get your phone number here////////////////////////                      

 for (int j = 0; j < json.length(); j++) {
JSONObject jo = json.getJSONObject(j);
if(arg0.getTitle().equals(jo.getString("title"))){
String phone=  jo.getString("ph");

}


///////////////you will get your phone number here////////////////////////         


Intent myIntent = new Intent(getBaseContext(),
                                DtActivity.class);
                        myIntent.putExtra("title", arg0.getTitle());
                        myIntent.putExtra("detail", arg0.getSnippet());
                        myIntent.putExtra("ph1", phone);



                        startActivity(myIntent);
                    }
                });

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

答案 2 :(得分:0)

问题是您要设置新的setInfoWindowAdaptersetOnInfoWindowClickListener 每次迭代到你的循环,最后一个迭代或最后一个json对象将是InfoWindowAdapterInfoWindowClickListener ,这就是为什么你得到最后一个json ,你只能设置一次不是多次,除了添加标记。

请勿将setInfoWindowAdaptersetOnInfoWindowClickListener放入for循环