Search in sources :

Example 1 with Book

use of com.codepath.android.booksearch.models.Book in project android-booksearch-exercise by codepath.

the class BookAdapter method onBindViewHolder.

// Involves populating data into the item through holder
@Override
public void onBindViewHolder(BookAdapter.ViewHolder viewHolder, int position) {
    // Get the data model based on position
    Book book = mBooks.get(position);
    // Populate data into the template view using the data object
    viewHolder.tvTitle.setText(book.getTitle());
    viewHolder.tvAuthor.setText(book.getAuthor());
    Glide.with(getContext()).load(Uri.parse(book.getCoverUrl())).placeholder(R.drawable.ic_nocover).into(viewHolder.ivCover);
// Return the completed view to render on screen
}
Also used : Book(com.codepath.android.booksearch.models.Book)

Example 2 with Book

use of com.codepath.android.booksearch.models.Book in project android-booksearch-exercise by codepath.

the class BookListActivity method fetchBooks.

// Executes an API call to the OpenLibrary search endpoint, parses the results
// Converts them into an array of book objects and adds them to the adapter
private void fetchBooks(String query) {
    client = new BookClient();
    client.getBooks(query, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            try {
                JSONArray docs;
                if (response != null) {
                    // Get the docs json array
                    docs = response.getJSONArray("docs");
                    // Parse json array into array of model objects
                    final ArrayList<Book> books = Book.fromJson(docs);
                    // Remove all books from the adapter
                    abooks.clear();
                    // Load model objects into the adapter
                    for (Book book : books) {
                        // add book through the adapter
                        abooks.add(book);
                    }
                    bookAdapter.notifyDataSetChanged();
                }
            } catch (JSONException e) {
                // Invalid JSON format, show appropriate error.
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
        }
    });
}
Also used : BookClient(com.codepath.android.booksearch.net.BookClient) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Header(cz.msebera.android.httpclient.Header) JSONObject(org.json.JSONObject) Book(com.codepath.android.booksearch.models.Book) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler)

Aggregations

Book (com.codepath.android.booksearch.models.Book)2 BookClient (com.codepath.android.booksearch.net.BookClient)1 JsonHttpResponseHandler (com.loopj.android.http.JsonHttpResponseHandler)1 Header (cz.msebera.android.httpclient.Header)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1