Skip to content

Commit

Permalink
Myndir komnar
Browse files Browse the repository at this point in the history
  • Loading branch information
helgifr committed Mar 14, 2018
1 parent d901ad3 commit d03fef2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.content.Context;
import com.example.gudmundurorripalsson.hvaderibio.Movie;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

Expand Down Expand Up @@ -162,18 +164,16 @@ public void run() {
}
});
try {
JSONArray jsonData = new JSONArray(response.body().string());
Log.v(TAG, jsonData.getJSONObject(0).getString("title"));
final JSONArray jsonData = new JSONArray(response.body().string());
if (response.isSuccessful()) {
// mForecast = parseForecastDetails(jsonData);
//We are not on main thread
//Need to call this method and pass a new Runnable thread
//to be able to update the view.
runOnUiThread(new Runnable() {
@Override
public void run() {
updateMovieList(jsonData);
//Call the method to update the view.
//updateDisplay();
}
});
} else {
Expand All @@ -200,6 +200,42 @@ private boolean isNetworkAvailable() {
if (networkInfo != null && networkInfo.isConnected()) isAvailable = true;
return isAvailable;
}

private void updateMovieList(JSONArray json) {
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listiMyndir);

Movie[] movies = new Movie[json.length()];
for (int i = 0; i < json.length(); i++) {
try {
JSONObject j = json.getJSONObject(i);
Movie movie = new Movie(
j.getInt("id"),
j.getString("title"),
j.getJSONObject("ratings").getString("imdb"),
j.getString("poster")
);
movies[i] = movie;
} catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}

String[] titles = new String[movies.length];
for (int i = 0; i < movies.length; i++) {
titles[i] = movies[i].getTitle();
if (movies[i].getImdb() != "null") {
titles[i] += " " + movies[i].getImdb();
}
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, titles);


// Assign adapter to ListView
listView.setAdapter(adapter);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class Movie {
public String poster;

public Movie(int id, String title, String imdb, String poster){
id = this.id;
title = this.title;
imdb = this.imdb;
poster = this.poster;
this.id = id;
this.title = title;
this.imdb = imdb;
this.poster = poster;
}

public int getId(){
Expand Down

0 comments on commit d03fef2

Please sign in to comment.