阵列适配器" getView()"没有被召唤

时间:2017-10-21 11:27:28

标签: java android adapter loader

我无法弄明白,为什么' getView'方法,在BookAdapter类中,永远不会被调用。好像我陷入了循环或其他什么,调试无法帮助我._。

以下是我的相关课程:

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;

   }
}

和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;

}}

每当我运行它时,它似乎会在&#34; onLoadFinished()&#34;之后卡住。方法,在mainActivity类中。到目前为止的一切都很好。从JSON解析工作完美无缺,返回的List看起来也很好,并且updateUI方法运行没有失败。

如果有人愿意深入挖掘我的代码并指出,我做错了什么,我会非常感激。

0 个答案:

没有答案