Android Studio 图书搜索 Google API
Posted
技术标签:
【中文标题】Android Studio 图书搜索 Google API【英文标题】:Android Studio Book search Google API 【发布时间】:2020-07-08 11:48:00 【问题描述】:OK 这个 android (Java) 应用程序搜索 Google App API 并在列表视图中显示书籍。它使用自定义适配器和异步加载器。我有一个应用程序的骨架,但只有一次搜索。之后,一切都停留在屏幕上,新的搜索没有发生,或者似乎没有发生。
我让它在创建时从 MainActivity 工作,但随后将其移至按钮和 EditText。
我认为问题主要出在这个文件中。
如果有人可以提供帮助,那就太好了!查看 goQuery 按钮单击下的最后一部分。 谢谢。
公共类 MainActivity 扩展 AppCompatActivity 实现 LoaderCallbacks> 私有静态最终字符串 LOG_TAG = MainActivity.class.getName(); 私有字符串 GOOGLE_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=android&maxResults=10"; /** * Book loader ID 的常量值。我们可以选择任何整数。 * 这真的只有在你使用多个加载器时才会起作用。 */ 私有静态最终 int BOOK_LOADER_ID = 1;
/** Adapter for the list of books */
private BookWormAdapter mAdapter;
/** TextView that is displayed when the list is empty */
private TextView mEmptyStateTextView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView bookwormListView = (ListView) findViewById(R.id.list);
mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
bookwormListView.setEmptyView(mEmptyStateTextView);
// Create a new adapter that takes an empty list of books as input
mAdapter = new BookWormAdapter(this, new ArrayList<Book>());
// Set the adapter on the @link ListView
// so the list can be populated in the user interface
bookwormListView.setAdapter(mAdapter);
/// End Button search
// Set an item click listener on the ListView, which sends an intent to a web browser
// to open a website with more information about the selected book.
bookwormListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
// Find the current book that was clicked on
Book currentBook = mAdapter.getItem(position);
// Convert the String URL into a URI object (to pass into the Intent constructor)
Uri bookwormUri = Uri.parse(currentBook.getPreviewLink());
// Create a new intent to view the book URI
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, bookwormUri);
// Send the intent to launch a new activity
startActivity(websiteIntent);
);
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
@Override
public Loader<List<Book>> onCreateLoader(int i, Bundle bundle)
// Create a new loader for the given URL
//Log.i(LOG_TAG, "onCreateLoader: TEST Created Loader!");
return new BookWormLoader(this, GOOGLE_REQUEST_URL);
@Override
public void onLoadFinished(Loader<List<Book>> loader, List<Book> bookworms)
// Hide loading indicator because the data has been loaded
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
// Log.i(LOG_TAG, "onLoadFinished: On Load finished!");
// Set empty state text to display "No books found."
mEmptyStateTextView.setText(R.string.no_books);
// Clear the adapter of previous book data
mAdapter.clear();
// If there is a valid list of @link Books, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (bookworms != null && !bookworms.isEmpty())
mAdapter.addAll(bookworms);
//mAdapter.notifyDataSetChanged();
@Override
public void onLoaderReset(Loader<List<Book>> loader)
// Loader reset, so we can clear out our existing data.
mAdapter.clear();
// Log.i(LOG_TAG, "onLoaderReset: TEST On Loader Reset!");
public void goQuery(View view)
EditText mySearchTextView = findViewById(R.id.searchEdit);
`enter code here`if (mySearchTextView.getText().length() < 1) return;
//mAdapter.clear();
// mAdapter.notifyDataSetChanged();
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.VISIBLE);
GOOGLE_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q="+mySearchTextView.getText()+"&maxResults=20";
// Get a reference to the ConnectivityManager to check state of network connectivity
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// Get details on the currently active default data network
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If there is a network connection, fetch data
if (networkInfo != null && networkInfo.isConnected())
// Get a reference to the LoaderManager, in order to interact with loaders.
android.app.LoaderManager loaderManager = getLoaderManager();
// 初始化加载器。传入上面定义的 int ID 常量并传入 null for // 捆绑包。为 LoaderCallbacks 参数传入此活动(有效 // 因为这个活动实现了 LoaderCallbacks 接口)。 loaderManager.initLoader(BOOK_LOADER_ID, null, this); 别的 // 否则显示错误 // 首先,隐藏加载指示器,以便显示错误消息 loadingIndicator.setVisibility(View.GONE);
// Update empty state with no connection error message
mEmptyStateTextView.setText(R.string.no_internet_connection);
【问题讨论】:
【参考方案1】:我将它添加到 buttonClick 中,现在它正在工作。不知道这是不是真的 Kosher?
loaderManager.destroyLoader(BOOK_LOADER_ID);
mAdapter.clear();
mAdapter.notifyDataSetChanged()enter code here
;
【讨论】:
以上是关于Android Studio 图书搜索 Google API的主要内容,如果未能解决你的问题,请参考以下文章
Android Gradle 插件自定义 Gradle 任务 ⑯ ( 从任务容器 TaskContainer 中搜索 Gradle 任务 | 压缩 packageDebug 任务输出文件 )