use of com.restart.spacestationtracker.adapter.LocationAdapter in project Space-Station-Tracker by Kiarasht.
the class Locations method displayPasses.
/**
* After successfully getting a city and country from the last JSON parsing, search a database
* to see when ISS will pass by this city, country.
*/
public List<Date> displayPasses(final String latitude, final String longitude, final Context applicationContext) {
// Used for Alert service
final List<Date> passes = new ArrayList<>();
final String url;
if (latitude == null && longitude == null) {
// Location.java is calling this method
url = "http://api.open-notify.org/iss-pass.json?lat=" + mLatitude + "&lon=" + mLongitude + "&n=20";
} else {
// Alert.java is calling this method
url = "http://api.open-notify.org/iss-pass.json?lat=" + latitude + "&lon=" + longitude;
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
final JSONArray results = response.getJSONArray("response");
final List<SightSee> dates = new ArrayList<>();
for (int i = 0; i < results.length(); ++i) {
final JSONObject aPass = results.getJSONObject(i);
passes.add(new Date(Long.parseLong(aPass.getString("risetime")) * 1000L));
final SightSee aSightSee = new SightSee(aPass.getInt("duration"), aPass.getInt("risetime"));
dates.add(aSightSee);
}
final View headerView = LayoutInflater.from(mActivity).inflate(R.layout.locations_header, null);
headerView.post(new Runnable() {
@Override
public void run() {
headerView.getLayoutParams().height = mFlexibleSpaceImageHeight;
}
});
mAdapter = new LocationAdapter(mActivity, headerView);
mAdapter.setDataSet(dates);
mRecyclerView.setAdapter(mAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
if (latitude == null && longitude == null) {
Toast.makeText(Locations.this, R.string.errorConnection, Toast.LENGTH_LONG).show();
}
}
});
// If Alert.java called us
if (requestQueue == null) {
RequestQueue requestQueue = Volley.newRequestQueue(applicationContext);
requestQueue.add(jsonObjectRequest);
} else {
// If Locations.java called us
jsonObjectRequest.setTag(TAG);
requestQueue.add(jsonObjectRequest);
}
// Only Alert.java benefits from this return
return passes;
}
Aggregations