2015-01-19 4 views
-2

변수가있는 ObjectOfRoutes 클래스와 그 get set 메서드가 있고 또한 웹 서비스를 구문 분석하고 ObjectOfRoutes 개체를 arraylist (ArrayList)로 설정하고 해당 arraylist를 전달하려고하는 DirectionParser 클래스가 있습니다. 다른 활동을 내가 각 목록보기 행을 클릭하면. (asynctask있는 webservice 구문 분석하고 listview onItemClick() 함수를 실행합니다.) 그 ObjectOfRoutes Parcelable 구현하지만 시도 int 여분의 arrayList null 때 디버깅 할 때 말한다.분해 할 수있는 문제

아무도 도와 줄 수 있습니까?

+1

도와 드릴 수있는 코드를 보여줄 수 있습니까? –

+0

코드를 게시 할 수 있습니까? – user1563671

답변

0

ObjectOfRoutes

public class ObjectsOfResults { 

private String name; 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 


private String vicinity; 
public String getVicinity() { 
    return vicinity; 
} 

public void setVicinity(String vicinity) { 
    this.vicinity = vicinity; 
} 


private String type; 
public String getType() { 
    return type; 
} 

public void setType(String type) { 
    this.type = type; 
} 


private String geometry; 
public String getGeometry() { 
    return geometry; 
} 

public void setGeometry(String geometry) { 
    this.geometry = geometry; 
} 


private String location; 
public String getLocation() { 
    return location; 
} 

public void setLocation(String location) { 
    this.location = location; 
} 

private double lat; 
public double getLat() { 
    return lat; 
} 

public void setLat(double lat) { 
    this.lat = lat; 
} 

private double lng; 
public double getLng() { 
    return lng; 
} 

public void setLng(double lng) { 
    this.lng = lng; 
} 

private String icon; 
public String getIcon() { 
    return icon; 
} 

public void setIcon(String icon) { 
    this.icon = icon; 
} 


private String reference; 
public String getReference() { 
    return reference; 
} 

public void setReference(String reference) { 
    this.reference = reference; 
} 

private String id; 
public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 


private String place_id; 
public String getPlace_id() { 
    return place_id; 
} 

public void setPlace_id(String place_id) { 
    this.place_id = place_id; 
} 

private String scope; 
public String getScope() { 
    return scope; 
} 

public void setScope(String scope) { 
    this.scope = scope; 
} 

private String status; 
public String getStatus() { 
    return status; 
} 

public void setStatus(String status) { 
    this.status = status; 
} 
} 



public class DirectionsParser { 

public static int stepsLength; 

public ArrayList<ObjectsOfRoutes> putObjintoArrayList(String url){ 


    ArrayList<ObjectsOfRoutes> routesList=new ArrayList<ObjectsOfRoutes>(); 

    JSONObject jsonObject=getJSONObjectFromUrl(url); 
    ObjectsOfRoutes routesObject=new ObjectsOfRoutes(); 

    try { 
     JSONArray jsonArray=jsonObject.getJSONArray(Constants.ROUTES); 

     System.out.println("uuuuuuuuuuuuuu"+jsonArray.length()); 
     JSONObject jObject=jsonArray.getJSONObject(0); 

     routesObject.setOvwPolyPoints(jObject.getJSONObject(Constants.OVERVIEW_POLYLINE) 
       .getString(Constants.POINTS)); 
     System.out.println(routesObject.getOvwPolyPoints()); 

     routesObject.setSummary(jObject.getString(Constants.SUMMARY)); 
     System.out.println("Summary:"+routesObject.getSummary()); 

     JSONArray jArray=jObject.getJSONArray(Constants.LEGS); 
     JSONObject jObject1=jArray.getJSONObject(0); 

     routesObject.setlDistanceText(jObject1.getJSONObject(Constants.DISTANCE).getString(Constants.TEXT)); 
     System.out.println(routesObject.getlDistanceText()); 
     routesObject.setlDurationText(jObject1.getJSONObject(Constants.DURATION).getString(Constants.TEXT)); 
     System.out.println(routesObject.getlDurationText()); 
     routesObject.setEndAddress(jObject1.getString(Constants.END_ADDRESS)); 
     routesObject.setEndLocLat(jObject1.getJSONObject(Constants.END_LOCATION).getDouble(Constants.LATITUDE)); 
     routesObject.setEndLocLng(jObject1.getJSONObject(Constants.END_LOCATION).getDouble(Constants.LONGITUDE)); 
     routesObject.setStartAddress(jObject1.getString(Constants.START_ADDRESS)); 
     routesObject.setlStartLocLat(jObject1.getJSONObject(Constants.START_LOCATION).getDouble(Constants.LATITUDE)); 
     routesObject.setlStartLocLng(jObject1.getJSONObject(Constants.START_LOCATION).getDouble(Constants.LONGITUDE)); 



     JSONArray stepsArray =jObject1.getJSONArray(Constants.STEPS); 
     ArrayList<JSONObject> stepsJObjectList=new ArrayList<JSONObject>(); 

     for(int x=0;x<stepsArray.length();x++){ 

      stepsJObjectList.add(stepsArray.getJSONObject(x)); 

      ObjectsOfRoutes stepsObject=new ObjectsOfRoutes(); 

      stepsObject.setsDistanceText(stepsJObjectList.get(x) 
        .getJSONObject(Constants.DISTANCE).getString(Constants.TEXT)); 
      System.out.println(stepsObject.getsDistanceText()); 

      stepsObject.setsDurationText(stepsJObjectList.get(x) 
        .getJSONObject(Constants.DURATION).getString(Constants.TEXT)); 
      System.out.println(stepsObject.getsDurationText()); 

      stepsObject.setsEndLocationLat(stepsJObjectList.get(x) 
        .getJSONObject(Constants.END_LOCATION).getDouble(Constants.LATITUDE)); 
      System.out.println(stepsObject.getsEndLocationLat()); 

      stepsObject.setsEndLocationLng(stepsJObjectList.get(x) 
        .getJSONObject(Constants.END_LOCATION).getDouble(Constants.LONGITUDE)); 
      System.out.println(stepsObject.getsEndLocationLng()); 

      stepsObject.setsHtml_instructions(stepsJObjectList.get(x) 
        .getString(Constants.HTML_INSTRUCTIONS).replaceAll("<[^>]*>", "")); 
      System.out.println(stepsObject.getsHtml_instructions()); 

      stepsObject.setsPoints(stepsJObjectList.get(x) 
        .getJSONObject(Constants.POLYLINE).getString(Constants.POINTS)); 
      System.out.println(stepsObject.getsPoints()); 

      stepsObject.setsStartLocLat(stepsJObjectList.get(x) 
        .getJSONObject(Constants.START_LOCATION).getDouble(Constants.LATITUDE)); 
      System.out.println("start Lat:"+stepsObject.getsStartLocLat()); 

      stepsObject.setsStartLocLng(stepsJObjectList.get(x) 
        .getJSONObject(Constants.START_LOCATION).getDouble(Constants.LONGITUDE)); 
      System.out.println("start longi:"+stepsObject.getsStartLocLng()); 

      stepsObject.setsTravelMode(stepsJObjectList.get(x).getString(Constants.TRAVEL_MODE)); 
      System.out.println("travel mode:"+stepsObject.getsTravelMode()); 

      routesList.add(stepsObject); 

     } 

     stepsLength=stepsArray.length(); 
     System.out.println("STEPSSS"+stepsLength); 

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

    return routesList; 

} 

public String readAll(Reader reader){ 
    StringBuilder sBuilder=new StringBuilder(); 
    int cp; 
    try { 
     while((cp=reader.read())!=-1){ 
      sBuilder.append((char)cp); 
     } 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return sBuilder.toString(); 
} 

public JSONObject getJSONObjectFromUrl(String url){ 

    JSONObject jsonObject=null; 

    try { 
     InputStream inputStream = new URL(url).openStream(); 
    BufferedReader bufferedReader= 
      new BufferedReader(new InputStreamReader(inputStream,Charset.forName("UTF-8"))); 
    String jsonText=readAll(bufferedReader); 
    jsonObject=new JSONObject(jsonText); 
    }catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return jsonObject; 

    } 


} 


    public class AVMActivity extends Activity { 

ListView listViewOfAvm; 
double lat; 
double longi; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.avm_activity_layout); 
    listViewOfAvm=(ListView)findViewById(R.id.listOfAvm); 
    LoadingPlacesDataFromJson loading=new LoadingPlacesDataFromJson(); 

    Bundle extras=getIntent().getExtras(); 
    lat=extras.getDouble("latitude"); 
    longi=extras.getDouble("longitude"); 
    String keyword=extras.getString("keyword"); 
    loading.execute("https://maps.googleapis.com/maps/api/place/search/json?keyword="+keyword+"&location="+lat+","+longi+"&radius=3000&key=API_KEY"); 

    listViewOfAvm.setOnItemClickListener(loading); 

} 

private class LoadingPlacesDataFromJson extends AsyncTask<String , Void, ArrayList<ObjectsOfResults>> 
               implements ListView.OnItemClickListener{ 

    double directionsLat; 
    double directionsLng; 
    ProgressDialog pDialog; 
    ArrayList<ObjectsOfResults> arrayListPlaces; 
    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
    pDialog=ProgressDialog.show(AVMActivity.this, "Lutfen Bekleyin!!!", "Avmler Yukleniyor..."); 
    } 

    @Override 
    protected ArrayList<ObjectsOfResults> doInBackground(String... params) { 
     // TODO Auto-generated method stub 
    JSONParser jParser=new JSONParser(); 
    return jParser.putObjintoArrayList(params[0]); 
    } 

    @Override 
    protected void onPostExecute(ArrayList<ObjectsOfResults> result) { 
     // TODO Auto-generated method stub 

     listViewOfAvm.setAdapter(new AVMListViewAdapter(getApplicationContext(), result)); 
     pDialog.dismiss(); 
     arrayListPlaces=result; 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 
     directionsLat=arrayListPlaces.get(position).getLat(); 
     directionsLng= arrayListPlaces.get(position).getLng(); 
     LoadingDirectionsData loadingDirections=new LoadingDirectionsData(); 
     loadingDirections.execute("https://maps.googleapis.com/maps/api/directions/json?origin="+lat+","+longi+"&destination="+directionsLat+","+directionsLng+"&language=tr&key=API_KEY"); 
     Intent intent=new Intent(getApplicationContext(),LocationViewWithVicinity.class); 
     intent.putExtra(Constants.LATITUDE, arrayListPlaces.get(position).getLat()); 
     intent.putExtra(Constants.LONGITUDE, arrayListPlaces.get(position).getLng()); 
     intent.putExtra(Constants.VICINITY, arrayListPlaces.get(position).getVicinity()); 

     intent.putExtra("HTML", loadingDirections.arrayList.get(position).getsHtml_instructions()); 
     startActivity(intent); 

    } 

} 

private class LoadingDirectionsData extends AsyncTask<String, Void,ArrayList<ObjectsOfRoutes>> 
             { 
    DirectionsParser directionParser; 
    ArrayList<ObjectsOfRoutes> arrayList; 
    @Override 
    protected ArrayList<ObjectsOfRoutes> doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     directionParser=new DirectionsParser(); 
     arrayList =directionParser.putObjintoArrayList(params[0]); 



     return arrayList; 
    } 


    } 
} 
+0

나는 onItemClick() intent.putParcelableArrayList ("arrayList", loadingDirecitons.arrayList);를 전달하려고합니다. LocationViewWithVicinity 클래스에서 수신하지만 null을 반환합니다. serializable whath 구현 후 – user1563671

0

는 Serializable를 가진 모델 클래스를 구현합니다. 본

intent.putExtra ("배열",의 ArrayList)처럼 ArrayList를 전달할 수 직렬화

같은 클래스 ObjectsOfResults을 이다;

그리고 당신이

getIntent() getSerializableExtra ("배열")와 같은 다른 활동에 동일하게 검색 할 수 있습니다.;

+0

조금 더 설명 할 수 있습니까? – user1563671

+0

arraylist가 null이 아닌지 확인하십시오. 먼저 –

+0

이 제어하는지 확인하십시오. 다른 활동에 전달할 값이 있는지 확인하십시오. – user1563671

0

귀하의 질문을 정확하게 이해하는 데 어려움이 있습니다. 그러나 test project에있는 활동간에 ArrayList를 전달하는 예를 찾을 수 있습니다.