我正在使用zxing barcode scanner扫描代码,并尝试获取结果并将其放入searchView
中并过滤recyclerView
。
扫描的代码出现在searchView
内部,并且触发了onQueryTextChange
事件,但没有进行过滤。当我直接输入searchView
时,它可以正常工作。我在做什么错了?
警告,我真的很陌生,所以请放轻松。
预先感谢
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search_scanner, menu);
MenuItem itemSearch = menu.findItem(R.id.action_search);
searchView = (SearchView) itemSearch.getActionView();
mSearchMenu = itemSearch;
ImageView closeButton = searchView.findViewById(R.id.search_close_btn);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
zoneProductsRvAdapter.getFilter().filter(newText);
return true;
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Find EditText view
EditText et = findViewById(R.id.search_src_text);
//Clear the text from EditText view
et.setText("");
//Clear query
searchView.setQuery("", false);
//Collapse the action view
searchView.onActionViewCollapsed();
//Collapse the search widget
mSearchMenu.collapseActionView();
}
});
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
int id = item.getItemId();
if (id == R.id.itemScanner){
IntentIntegrator integrator = new IntentIntegrator(ZoneProductsActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.setOrientationLocked(true);
integrator.initiateScan();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
switch (requestCode) {
case 49374:
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null){
if (result.getContents() != null){
String ScannedCode = result.getContents();
searchView.onActionViewExpanded();
searchView.setIconified(false);
searchView.setQuery(ScannedCode, false);
searchView.clearFocus();
}
}
break;
}
}