Java 我需要获取用户在searchView中输入的文本,以更新URL查询

Java 我需要获取用户在searchView中输入的文本,以更新URL查询,java,android,Java,Android,因此,这里我需要获取用户在搜索视图中输入的文本,以便能够更新GOOGLE\u BOOKS\u REQUEST\u URL public class BookListingActivity extends AppCompatActivity implements LoaderCallbacks<List<BookListing>> { private static final String LOG_TAG = BookListingActivity.class.g

因此,这里我需要获取用户在搜索视图中输入的文本,以便能够更新
GOOGLE\u BOOKS\u REQUEST\u 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;
            }
        });
公共类BookListingActivity扩展AppCompative实现LoaderCallbacks{
private static final String LOG_TAG=BookListingActivity.class.getName();
/**
*这是用户对API查询所做的搜索
*/
私有字符串用户查询;
/**
*来自google图书数据集的图书数据的URL
*/
私有字符串GOOGLE\u BOOKS\u REQUEST\u URL=
"https://www.googleapis.com/books/v1/volumes?q=“+用户查询;
/**
*书籍加载器ID的常量值。我们可以选择任何整数。
*这只有在您使用多个加载程序时才起作用。
*/
私有静态最终int BOOK_LOADER_ID=1;
/**
*书籍列表的适配器
*/
私人书单适配器;
/**
*列表为空时显示的文本视图
*/
私有文本视图mEmptyStateTextView;
/**
*加载列表时显示的进度栏
*/
私人ProgressBar mProgressBar;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找将在其中执行查询的searchView
SearchView查询=(SearchView)findViewById(R.id.search_按钮);
query.setOnQueryTextListener(新的SearchView.OnQueryTextListener(){
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回true;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
返回false;
}
});

您可能想要这样的东西:

    @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\u BOOKS\u REQUEST\u URL
变量,它不应该包含查询字符串(因为它一开始是空的,在那里没有意义)


这可能会有所帮助。我假设您希望使用回调
onquerytexsubmit
onquerytexchange
我非常感谢您的努力,但问题是我正在使用加载程序在后台线程上处理请求。所以我想我需要一种方法来更新变量,然后将其传递给onCreateLoader方法。或者我只是个哑巴,答案很明显吗?我只是在你的代码中注意到你的活动实现了LoaderManager.LoaderCallBacks。在这种情况下,你需要在每次输入另一个查询时重新启动你的加载程序。为此,你需要调用restartLoader。我修改了我编写的代码。让我知道你是否正在尝试这样做。最后一个问题stion,我现在在这里传递什么?@Override public Loader onCreateLoader(inti,Bundle Bundle){//为给定的URL创建一个新的加载程序返回新的BookLoader(this,GOOGLE_BOOKS_REQUEST_URL);}@例如,LeandervdWalt您可以返回的是一个AsyncTaskLoader。它将负责从后台的url获取数据,并在主线程上返回数据供您使用。下面是一篇更详细的stackoverflow帖子,解释了您想要做的事情Husayn,非常感谢buddy,您真的帮了大忙:D
private static final String GOOGLE_BOOKS_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=";