可分配的问题

时间:2015-01-19 09:48:49

标签: android arraylist parcelable

我有一个ObjectOfRoutes类,它有变量及其get set方法,我还有一个DirectionParser类,它解析Web服务并将ObjectOfRoutes对象设置为arraylist(ArrayList),我想将该arraylist传递给另一个activity当我点击每个listview行时。(我在asynctask中解析webservice并在listview的onItemClick()函数中执行它)我尝试使用ObjectOfRoutes实现Parcelable但是额外的intent表示我调试时arrayList为null。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分: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;
    }


   }
 }

答案 1 :(得分:0)

使用Serializable实现模型类。 这使得ObjectsOfResults类成为Serializable

你可以像这样传递arraylist

intent.putExtra(&#34; array&#34;,arraylist);

你可以在其他活动中检索相同的内容

getIntent()getSerializableExtra(&#34;阵列&#34);

答案 2 :(得分:0)

我很难完全理解您的问题,但您可以找到在此test project中的活动之间传递ArrayList的示例。