0

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.

Bjonic
  • 133
  • 10
  • So are you sure books are not null and not empty? :) is Log.i(log, "Everything fine"); actually called? – tjugg Oct 21 '17 at 11:31
  • have you override function getCount()? – Itzik Samara Oct 21 '17 at 11:35
  • thanks for your reply :) Yep, "Everything fine" shows up in the logcat and at this line "mBookAdapter = new BookAdapter(this, books);" books has the size of 10 – Bjonic Oct 21 '17 at 11:35
  • MainActivity extends or implement what? – Xenolion Oct 21 '17 at 11:35
  • No, i did not overwrite the getCount() function and @Xenolion I'm afraid I don't understand your question. – Bjonic Oct 21 '17 at 11:40
  • So what does adapter.getCount return? If it returns 0, or you have not overwriten this method, the getview will not get called, so theres your issue – tjugg Oct 21 '17 at 11:43
  • Show where you the line of your Activity starts because its not there for example `public MainActivity extends AppCompatActivity implement Something {` – Xenolion Oct 21 '17 at 11:45
  • 2
    Don't worry about `getCount()`. The `List` is being passed to the `super` constructor, so `ArrayAdapter` will handle the count correctly. Are you sure the `ListView` is visible in your layout? – Mike M. Oct 21 '17 at 11:49
  • @tjugg It does return 10 – Bjonic Oct 21 '17 at 11:51
  • @Xenolion, I'm sorry, I thought I've already posted that. I added it to the main post. – Bjonic Oct 21 '17 at 11:52
  • 1
    @MikeM. Oh holy god... Still had android:visibility="gone" in the ListView, cause I copy&pasted it from a previous project... I feel so dumb right now ._. Guess I learned a valuable lesson today... Thanks for pointing that out and thanks to everyone that posted here – Bjonic Oct 21 '17 at 11:56
  • Hope you are good now! – Xenolion Oct 21 '17 at 12:01

0 Answers0