我的主要目标是使用SearchView中的查询显示(网格)书籍列表(在我的代码中称为BookObjects)。我的代码实际上使用Google Books API从URL解析JSON。 我实现的代码修改了searchBox中给定查询的http链接。 (它使用ArrayList和自定义适配器以及专为我实现的BookObjects构建的自定义Loader。)
我面临的问题如下:
第一次搜索顺利(gridview从查询字中正确膨胀)但是当我在searchBox中重新键入keyworks时,我的gridView永远不会刷新。它与第一次搜索时的当前项目保持一致。
我在主要活动中的当前代码如下所示:
public class UserActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<BookObject>> {
public String http_link;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
...
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.menuSearch);
SearchView searchView = (SearchView) searchItem.getActionView();
final GridView booksListView = (GridView) findViewById(R.id.GridList);
mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
booksListView.setEmptyView(mEmptyStateTextView);
mAdapter = new BookAdapter(UserActivity.mMainActivity, new ArrayList<BookObject>());
final LoaderManager loaderManager = getLoaderManager();
searchView.setOnQueryTextListener(
new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
//check if device is connected to network
if (isNetworkConnected() == true) {
http_link = "https://www.googleapis.com/books/v1/volumes?q=" + query + "&maxResults=15";
Log.d(http_link, "link is:");
Log.d(mInputText, "link is:");
//Tried them both and had no success
// mAdapter.clear();
// mAdapter.notifyDataSetChanged();
loaderManager.initLoader(BOOK_LOADER_ID, null, UserActivity.mMainActivity);
booksListView.setAdapter(mAdapter);
} else {
mEmptyStateTextView.setText("No internet connection found.");
}
return false;
}
}
);
return super.onCreateOptionsMenu(menu);
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
这些可以在我的自定义BookObject Loader中找到:
(发布它们以便您了解当我调用Loader时会发生什么)
@Override
public Loader<List<BookObject>> onCreateLoader(int i, Bundle bundle) {
// Create a new loader for the given URL
return new BookLoader(this, http_link);
}
@Override
public void onLoadFinished(Loader<List<BookObject>> loader, List<BookObject> books) {
// Clear the adapter of previous earthquake data
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
mAdapter.clear();
mEmptyStateTextView.setText("No books found");
// If there is a valid list of {@link BookObject}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (books != null && !books.isEmpty()) {
mAdapter.addAll(books);
}
}
@Override
public void onLoaderReset(Loader<List<BookObject>> loader) {
// Loader reset, so we can clear out our existing data.
mAdapter.clear();
}
这是适配器类代码:
public class BookAdapter extends ArrayAdapter<BookObject> {
public BookAdapter(Activity context , List<BookObject> books)
{
super(context, 0 , books); // 2nd par is 0 because we inflate manually
}
@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.books_grid_list, parent, false);
}
BookObject currentBook = getItem(position);
TextView TitleTextView = (TextView) listItemView.findViewById(R.id.Title_text_view);
TitleTextView.setText(currentBook.getTitle());
TextView SubtitleTextView = (TextView) listItemView.findViewById(R.id.Subtitle_text_view);
SubtitleTextView.setText(currentBook.getSubtitle());
TextView AuthorsTextView = (TextView) listItemView.findViewById(R.id.Authors_text_view);
AuthorsTextView.setText(currentBook.getAuthors());
ImageView ThumbnailImageView = (ImageView) listItemView.findViewById(R.id.book_image_id);
Picasso.with(UserActivity.mMainActivity).load(currentBook.getImageURL()).into(ThumbnailImageView);
return listItemView;
}
}
我在网上搜索了解决方案,我尝试添加onCreateOptionsMenu - &gt; onQueryTextSubmit方法不同的行如:(一次一个)
mAdapter.clear();
mAdapter.notifyDataSetChanged();
你能告诉我我错了什么吗?
刷新:我的GridView在第一次查询后永远不会更新。
注意:我通过Logs检查了链接(http_link)是否使用第二个(第3个第4个...等)搜索中的新查询进行了更新。
因此问题在于我的GridView无法使用新项目进行更新。
我真的无法弄明白,任何能引导我走上正确道路的建议都会受到高度赞赏。 (现在已经挣扎了5-6个小时)
提前谢谢。
答案 0 :(得分:1)
我觉得我发现了问题。它与装载机。从Loader LoaderManager.onInitLoader的文档中,如果已经创建了加载器,那么它只使用相同的加载器并直接调用onLoadFinished()
。
因此,在onQueryTextSubmit()
{而不是致电initLoader()
致电restartLoader()
loaderManager.restartLoader(BOOK_LOADER_ID, null, UserActivity.mMainActivity);