Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.guidebook.challenge;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.widget.ListView;
- import com.android.volley.Request.Method;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.VolleyLog;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.guidebook.challenge.adapter.CustomListAdapter;
- import com.guidebook.challenge.app.AppController;
- import com.guidebook.challenge.model.Event;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends Activity {
- // Log tag
- private static final String TAG = MainActivity.class.getSimpleName();
- // Movies json url
- private static final String url = "https://guidebook.com/service/v2/upcomingGuides/";
- private ProgressDialog pDialog;
- private List<Event> eventList = new ArrayList<Event>();
- private ListView listView;
- private CustomListAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listView = (ListView) findViewById(R.id.list);
- adapter = new CustomListAdapter(this, eventList);
- listView.setAdapter(adapter);
- pDialog = new ProgressDialog(this);
- // Showing progress dialog before making http request
- pDialog.setMessage("Loading...");
- pDialog.show();
- // changing action bar color
- /*getActionBar().setBackgroundDrawable(
- new ColorDrawable(Color.parseColor("#1b1b1b")));*/
- // Creating volley request obj
- JsonObjectRequest movieReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>(){
- @Override
- public void onResponse(JSONObject response) {
- Log.d(TAG, response.toString());
- hidePDialog();
- System.out.println("lol");
- // Parsing json
- for (int i = 0; i < response.length(); i++) {
- try {
- JSONObject obj = response.getJSONObject(String.valueOf(i));
- Event event = new Event();
- event.setStartDate(obj.getString("startDate"));
- Log.e("startDate", obj.getString("startDate"));
- event.setIconURL(obj.getString("icon"));
- event.setRating(((Number) obj.get("rating"))
- .doubleValue());
- event.setYear(obj.getInt("releaseYear"));
- // Genre is json array
- JSONArray genreArry = obj.getJSONArray("genre");
- ArrayList<String> genre = new ArrayList<String>();
- for (int j = 0; j < genreArry.length(); j++) {
- genre.add((String) genreArry.get(j));
- }
- event.setGenre(genre);
- // adding movie to movies array
- eventList.add(event);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- // notifying list adapter about data changes
- // so that it renders the list view with updated data
- adapter.notifyDataSetChanged();
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.d(TAG, "Error: " + error.getMessage());
- hidePDialog();
- }
- });
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(movieReq);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- hidePDialog();
- }
- private void hidePDialog() {
- if (pDialog != null) {
- pDialog.dismiss();
- pDialog = null;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment