1

This is my onClick information:

        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();

            }

    });

BookListFragment w/BookLoader:

    @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();

}

I am able to get search results. But when searching again the previous results don't clear. They just continuously overlap.

First Search Screenshot Second Search Screenshot

T. Rodgers
  • 13
  • 3

5 Answers5

2
 @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();
 }
0

When adding a BookListFragment, use replace() instead of add()

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

Hope it helps

camilocons
  • 31
  • 2
0

You have to use replace() instead of add()

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

That should do it

JAAD
  • 12,349
  • 7
  • 36
  • 57
0

This is one solution. Remove all child views (rows) by first getting their parent (ListView).

public void removeView(View v) {
    if(v.getParent() != null) {
        ((ViewGroup) v.getParent()).removeView(v);
    }
}

Call removeView(v) for every row in your ListView. In this case all rows share one parent so try making that parent an instance field and then method above will be:

ListView listView;

. . .

public static void removeView(View v) {
    listView.removeView(v);
}

You can also remove all rows with one single call by:

listView.removeAllViews();

Hope this helps!

jelic98
  • 723
  • 1
  • 12
  • 28
0

difference between fragmentTransaction.add and fragmentTransaction.replace Just further adding onto CamiloCons answer, you're "adding" a new layer of fragments on top everytime you call add().

whompum
  • 69
  • 2
  • 12