I'm unable to figure out, why the 'getView' method, in the BookAdapter class, never gets called. It seems like I'm stuck in a loop or something and debugging can't help me ._.
Here are my relevant classes:
MainActivity.java
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Book>>
String log = MainActivity.class.getName();
String googleAPI = "https://www.googleapis.com/books/v1/volumes?q=android&maxResults=10";
BookAdapter mBookAdapter;
ListView mBookListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results);
LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(0, null, this);
mBookAdapter = new BookAdapter(this, new ArrayList<Book>());
mBookListView = (ListView) findViewById(R.id.search_results);
}
@Override
public Loader<List<Book>> onCreateLoader(int i, Bundle bundle) {
return new BooksLoader(this, googleAPI);
}
@Override
public void onLoadFinished(Loader<List<Book>> loader, List<Book> books) {
if(!mBookAdapter.isEmpty()) {
mBookAdapter.clear();
Log.i(log, "!!Load finished and adapter cleared");
}
updateUI(books);
Log.i("MainActivity.java", "UI updated!");
}
@Override
public void onLoaderReset(Loader<List<Book>> loader) {
mBookAdapter.clear();
}
public void updateUI(List<Book> books){
if(books != null && !books.isEmpty()) {
mBookAdapter = new BookAdapter(this, books);
mBookListView.setAdapter(mBookAdapter);
Log.i(log, "Everything fine");
}
}
BooksLoader.java
public class BooksLoader extends AsyncTaskLoader{
String mURL;
BooksLoader(Context context, String url){
super(context);
mURL = url;
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public List<Book> loadInBackground() {
QueryUtils qu = new QueryUtils();
String APIreturn = qu.makeHttpRequest(mURL);
List<Book> books = qu.parseBooksFromJSON(APIreturn);
return books;
}
}
And the BooksAdapter.java
public class BookAdapter extends ArrayAdapter<Book> {
public BookAdapter(Context context, List<Book> books) {
super(context, 0, books);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.single_result, parent, false);
}
Book currentBook = getItem(position);
Log.i("BookAdapter.java", "Adapter loaded");
TextView bookName = (TextView) listItemView.findViewById(R.id.bookName);
bookName.setText(String.valueOf(currentBook.getmBookName()));
//TextViews für den Place vorbereiten und setzen.
TextView bookDescription = (TextView) listItemView.findViewById(R.id.bookDescription);
bookDescription.setText(currentBook.getmBookDescription());
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}}
Whenever i run it, it seems to get stuck after the "onLoadFinished()" method, in the mainActivity class. Everything till that point works fine. Parsing from JSON works flawless, the returned List looks good too, and the updateUI Method runs without failure.
If anyone is willing to dig through my code and point out, what I did wrong, I'd be very thankful.