这是我的onClick信息:

        Button searchButton = (Button) findViewById(R.id.search_button);
    searchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                BookListFragment bookListFragment = new BookListFragment();

                getSupportFragmentManager().beginTransaction()
                        .add(R.id.book_list_frame, bookListFragment).commit();

            }

    });


带有BookLoader的BookListFragment:

    @Override
public Loader<List<BookListing>> onCreateLoader(int i, Bundle args) {

    return new BookLoader(this, GOOGLE_BOOKS_URL + searchedBook);
}


@Override
public void onLoadFinished(Loader<List<BookListing>> loader, List<BookListing> bookListings) {
    mAdapter.clear();


    if(bookListings != null && !bookListings.isEmpty()) {
        mAdapter.addAll(bookListings);
    }

}

@Override
public void onLoaderReset(Loader<List<BookListing>> loader) {
    mAdapter.clear();

}


我能够得到搜索结果。但是,当再次搜索以前的结果时,不会清除。它们只是不断重叠。

First Search Screenshot
Second Search Screenshot

最佳答案

 @Override
 public Loader<List<BookListing>> onCreateLoader(int i, Bundle args) {

 return new BookLoader(this, GOOGLE_BOOKS_URL + searchedBook);
 }


@Override
public void onLoadFinished(Loader<List<BookListing>> loader,
    List<BookListing> bookListings) {
         mAdapter.clear();


if(bookListings != null && !bookListings.isEmpty()) {
    mAdapter.replace(bookListings);
    }

}
@Override
public void onLoaderReset(Loader<List<BookListing>> loader) {
mAdapter.clear();
 }

10-07 19:20
查看更多