2014-04-22 82 views
1

我在一個應用程序,其中我從我的遠程server.I得到響應(JSON)的工作我也能夠分析,並基於該response.But我是從響應和傳球在解析pH值數據失敗的位置標記它進入另一個活動。解析多個JSON對象值導致該對象中的最後一個值。

它只發送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

您需要初始化GMAP Globaly並添加for循環內的標記。刪除適配器,然後單擊窗體循環中的偵聽器代碼並將其粘貼到外部。請檢查下面的示例。

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. 
} 

}

請粘貼您Asyntask類的onPostExecute內循環。

+0

如果我這樣做,我怎麼能得到電話號碼出側循環..? 在onInfoWindowClickListner? – Aparichith

+0

非常感謝它的工作 – Aparichith

0

的問題是,您要設置每次迭代,以UR for循環,最後迭代或最後JSON對象時,一個新的setInfoWindowAdaptersetOnInfoWindowClickListener 將除添加標記的InfoWindowAdapterInfoWindowClickListener這就是你所得到的最後JSON,你只能設置一次不多次。

不要放在了setInfoWindowAdaptersetOnInfoWindowClickListener for循環

1

添加此代碼段拿到手機number.I已經打上它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(); 
     }