在启动Intent

时间:2018-06-15 05:08:53

标签: android android-studio android-intent

我有一个活动可以获得搜索查询以及用户想要查看的结果数量。我正在查询Google Books API,最大结果数为40。

如何设置如果用户输入的数字高于40,默认设置为10,然后启动活动,如果数字介于1到40之间,则会检索到多少被放入。

这是包含代码的Activity:

BookSearch.java 公共类BookSearch扩展了AppCompatActivity {

private String searchQuery;
private String numberQuery;

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

    Button searchBtn = (Button)findViewById(R.id.search_button);
    final SearchView searchView = (SearchView)findViewById(R.id.search_view);
    final EditText editText = (EditText)findViewById(R.id.edit_text);

    searchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchQuery = searchView.getQuery().toString();
            numberQuery = editText.getText().toString();

            Intent bookIntent = new Intent(BookSearch.this, MainActivity.class);
            bookIntent.putExtra("searchQuery",searchQuery);
            bookIntent.putExtra("numberQuery",numberQuery);

            startActivity(bookIntent);
        }
    });
}

}

MainActivity.java 这会收到意图并将其放入构造函数网址

public static final String LOG_TAG = MainActivity.class.getName();
private static final int BOOK_LOADER_ID = 1;

private static final String GOOGLE_BOOKS = "https://www.googleapis.com/books/v1/volumes?q=";
private static final String MAX_RESULTS = "&maxResults=";

private String searchURL;
private String intentNumber;
private String intentSearch;
private BookAdapter mAdapter;

private RecyclerView mRecyclerView;

private TextView mErrorTextView;

private ProgressBar mProgressBar;

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

    Intent intent = getIntent();
    intentSearch = intent.getExtras().getString("searchQuery");
    intentNumber = intent.getExtras().getString("numberQuery");

    searchURL = GOOGLE_BOOKS + intentSearch + MAX_RESULTS + intentNumber;

    final List<Book> books = new ArrayList<>();

    mRecyclerView = findViewById(R.id.recyclerView);
    mErrorTextView = findViewById(R.id.error_textView);
    mProgressBar = findViewById(R.id.progressBar);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);

    mAdapter = new BookAdapter(this, books);

    mRecyclerView.setAdapter(mAdapter);

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(BOOK_LOADER_ID, null, this);
    } else {
        mProgressBar.setVisibility(GONE);
        mErrorTextView.setText(R.string.no_internet_connection);
    }
}

@Override
public Loader<List<Book>> onCreateLoader(int id, Bundle args) {
    return new BookLoader(this, searchURL);
}

@Override
public void onLoadFinished(Loader<List<Book>> loader, List<Book> books) {
    mAdapter.clear();

    if (books != null && !books.isEmpty()) {
        mProgressBar.setVisibility(GONE);
        mErrorTextView.setVisibility(GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
        mAdapter.addAll(books);
    } else {
        mProgressBar.setVisibility(GONE);
        mRecyclerView.setVisibility(GONE);
        mErrorTextView.setVisibility(View.VISIBLE);
    }
}

@Override
public void onLoaderReset(Loader<List<Book>> loader) {
    mAdapter.clear();
}

}

1 个答案:

答案 0 :(得分:1)

将addTextChangedListener设置为您的Edittext并处理更多事情: -

numberQuery .addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable editable) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      try
      {
      int n=Integer.valueOf(s);
      if(n>=40)
         numberQuery.setText("10");

      }
      catch(Exception e)
      {
       //handle exception like NumberFormatException, etc.
      }
   }
  });