我需要获取用户放在searchView中的文本来更新URL查询

时间:2017-09-20 08:06:29

标签: java android

所以在这里我需要获取用户输入搜索视图的文本,以便能够更新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;
            }
        });

1 个答案:

答案 0 :(得分:0)

你想要这样的东西:

    @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=";