Skip to content

Instantly share code, notes, and snippets.

@sebi1995
Created May 15, 2017 09:34
Show Gist options
  • Save sebi1995/67ca1ee79537e081ccb6d16e6288026e to your computer and use it in GitHub Desktop.
Save sebi1995/67ca1ee79537e081ccb6d16e6288026e to your computer and use it in GitHub Desktop.
package com.example.zdroa.fetchmovies;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.TreeMap;
public class MainActivityFragment extends Fragment {
private static String API_KEY = "b692b9da86f1cf0c1b623ea6e2770101";
ListView listView;
Button button;
static TreeMap<Integer, Integer> IDS;
static TreeMap<Integer, String> TITLES;
static TreeMap<Integer, String> POSTERS;
static TreeMap<Integer, String> GENRES;
static TreeMap<Integer, String> PRODUCTION_COUNTRIES;
static TreeMap<Integer, String> OVERVIEW;
static TreeMap<Integer, String> RUNTIME;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) rootView.findViewById(R.id.lvMovies);
new ImageLoadTask().execute();
//if fragment is visible
if (getActivity() != null) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(TITLES.ceilingEntry(position).getValue());
}
});
}
return rootView;
}
@Override
public void onStart() {
super.onStart();
}
private class ImageLoadTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
IDS = new TreeMap<>();
IDS.put(1, 2);
IDS.put(2, 3);
IDS.put(3, 5);
IDS.put(4, 6);
IDS.put(5, 8);
IDS.put(6, 9);
IDS.put(7, 11);
IDS.put(8, 12);
IDS.put(9, 13);
IDS.put(10, 14);
populateTreeMaps();
return null;
}
@Override
protected void onPostExecute(Void result) {
if (getActivity() != null) {
ListViewAdaptor adapter = new ListViewAdaptor(getActivity(), POSTERS, TITLES);
listView.setAdapter(adapter);
System.out.println(POSTERS);
System.out.println(TITLES);
}
}
private Void populateTreeMaps() {
HttpHandler httpHandler = new HttpHandler();
TITLES = new TreeMap<>();
POSTERS = new TreeMap<>();
for (int i = 0; i < IDS.size(); i++) {
String jsonString = httpHandler.makeServiceCall("https://api.themoviedb.org/3/movie/" + IDS.ceilingEntry(i).getValue() + "?api_key=" + API_KEY);
if (jsonString != null) {
try {
//POPULATE ALL THE GRAPHS HERE
getTitles(jsonString, IDS.ceilingEntry(i).getValue());
getPosterURLs(jsonString, IDS.ceilingEntry(i).getValue());
} catch (JSONException e) {
return null;
}
}
}
return null;
}
private void getTitles(String JSONString, Integer mov_id) throws JSONException {
JSONObject jsonObject = new JSONObject(JSONString);
String title = jsonObject.getString("title");
TITLES.put(mov_id, title);
}
private void getPosterURLs(String JSONString, Integer mov_id) throws JSONException {
JSONObject jsonObject = new JSONObject(JSONString);
String url = jsonObject.getString("poster_path");
POSTERS.put(mov_id, url);
}
private void getYoutubeLink() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment