【问题标题】:I need to get the text the user puts in the searchView to update the URL query我需要获取用户在 searchView 中输入的文本以更新 URL 查询
【发布时间】:2018-03-01 04:37:17
【问题描述】:

所以在这里我需要获取用户输入到搜索视图中的文本,以便能够更新GOOGLE_BOOKS_REQUEST_URL

public class BookListingActivity extends AppCompatActivity implements LoaderCallbacks<List<BookListing>> {

    private static final String LOG_TAG = BookListingActivity.class.getName();

    /**
     * This is the search the user does for the API query
     */
    private String userQuery;
    /**
     * URL for book data from the google books dataset
     */
    private String GOOGLE_BOOKS_REQUEST_URL =
            "https://www.googleapis.com/books/v1/volumes?q=" + userQuery;
    /**
     * Constant value for the book loader ID. We can choose any integer.
     * This really only comes into play if you're using multiple loaders.
     */
    private static final int BOOK_LOADER_ID = 1;

    /**
     * Adapter for the list of books
     */
    private BookListingAdapter mAdapter;

    /**
     * TextView that is displayed when the list is empty
     */
    private TextView mEmptyStateTextView;

    /**
     * ProgressBar that is displayed when the list is loading
     */
    private ProgressBar mProgressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find the searchView in which the query will be performed
        SearchView query = (SearchView) findViewById(R.id.search_button);
        query.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

【问题讨论】:

标签: java android


【解决方案1】:

你会想要这样的东西:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find the searchView in which the query will be performed
        SearchView query = (SearchView) findViewById(R.id.search_button);
        query.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String url = GOOGLE_BOOKS_REQUEST_URL + query;
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                getLoaderManager().restartLoader(BOOK_LOADER_ID, bundle, BookListingActivity.this);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

这意味着您需要更新您的GOOGLE_BOOKS_REQUEST_URL 变量,它不应该包含查询字符串(因为它一开始是空的,所以没有任何意义)。

private static final String GOOGLE_BOOKS_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=";

【讨论】:

  • 我非常感谢您的努力,但问题是我正在使用加载程序来处理后台线程上的请求。所以我想我需要一种方法来更新变量,然后将其传递给 onCreateLoader 方法。还是我只是愚蠢而答案很明显?
  • 我刚刚在您的代码中注意到您的 Activity 实现了 LoaderManager.LoaderCallBacks。在这种情况下,每次输入另一个查询时,您都需要重新启动加载程序。为此,您需要调用 restartLoader。我修改了我写的代码。让我知道您是否正在尝试这样做。
  • 最后一个问题,我现在在这里传递什么? @Override public Loader> onCreateLoader(int i, Bundle bundle) { // 为给定的 URL 创建一个新的加载器 return new BookLoader(this, GOOGLE_BOOKS_REQUEST_URL); }
  • @LeandervdWalt 例如,您可以返回一个 AsyncTaskLoader。它将负责在后台从 url 获取数据,并将其返回到主线程供您使用。这是一个更详细的 stackoverflow 帖子,解释了与您想要做的相同的事情 stackoverflow.com/questions/41267446/…
  • 侯赛因,非常感谢你的朋友,你真的帮了大忙:D
猜你喜欢
  • 1970-01-01
  • 2019-06-13
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
相关资源
最近更新 更多