2017-10-12 56 views
1

这是我的onClick信息:BookListing项目。每本图书搜索都会覆盖以前的结果。你如何清除ListView?

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

} 

我能够得到的搜索结果。但是当再次搜索时,以前的结果不明确。他们只是不断重叠。

First Search Screenshot Second Search Screenshot

回答

0

当添加BookListFragment,使用replace()代替add()

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

希望它可以帮助

0

你必须使用replace()代替add()

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

应该这样做

+0

重复答案? – phpdroid

+0

@phpdroid我在写回复时他张贴 –

0

这是一个解决方案。通过首先获取其父(ListView)来删除所有子视图(行)。

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

对于ListView中的每一行调用removeView(v)。在这种情况下,所有的行共享一个家长这样尝试使该父实例字段,然后上面的方法将是:

listView.removeAllViews(); 

希望:

ListView listView; 

. . . 

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

您还可以通过删除与一个单一的电话都行这有助于!

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