好的 这个 Android ( Java ) 应用程序搜索 Google App API 并在列表视图中显示书籍。它使用自定义适配器和异步加载器。我有一个应用程序的骨架,但只有一次搜索。之后,一切都停留在屏幕上,新的搜索不会发生,或者似乎不会发生。
我让它在创建时从 MainActivity 工作,但随后将其移至按钮和 EditText。
我认为问题主要存在于这个文件中。
如果有人可以提供帮助,那就太好了!查看 goQuery 按钮单击下的最后一部分。谢谢。
公共类 MainActivity 扩展 AppCompatActivity 实现 LoaderCallbacks> { private static final String 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 Book}s, 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 常量,并为 // bundle 传入 null。为 LoaderCallbacks 参数传入此活动(这是有效的 // 因为此活动实现了 LoaderCallbacks 接口)。loaderManager.initLoader(BOOK_LOADER_ID, null, this); } else { // 否则,显示错误 // 首先,隐藏加载指示器,以便显示错误消息 loadingIndicator.setVisibility(View.GONE);
// Update empty state with no connection error message
mEmptyStateTextView.setText(R.string.no_internet_connection);
}
}
}