I have an SearchView and a button in an ActionBar. The button shows a DialogFragment that contains some checkboxes to filter search results. But when I click OK to close the DialogFragment, the SearchView query resets, and I don't know why. This is my code:
BusquedaActivity.java
:
public class BusquedaActivity extends AppCompatActivity implements FiltroTemasDialogFragment.FiltroTemasDialogListener {
RecyclerView rv;
LinearLayoutManager lm;
RecursoAdapter ra;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set actionbar
// Create RecyclerView
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.busqueda, menu);
MenuItem searchItem = menu.findItem(R.id.acc_buscar);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String cad) {
buscador(cad);
return true;
}
@Override
public boolean onQueryTextChange(String cad) { return true; }
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.acc_listar:
FiltroTemasDialogFragment dialogFragment = new FiltroTemasDialogFragment();
dialogFragment.show(getSupportFragmentManager(),"filtro_temas");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onDialogPositiveClick(FiltroTemasDialogFragment dialog) {
// ...
buscador(((SearchView) findViewById(R.id.acc_buscar)).getQuery().toString());
}
public void buscador(String cad){
// Search
ra.notifyDataSetChanged();
}
}
Thanks in advance.
CodePudding user response:
I think you can declare the SearchView
at the class. Then, you can refer it to get the text.
RecyclerView rv;
LinearLayoutManager lm;
RecursoAdapter ra;
private SearchView searchView;
Then change your:
SearchView searchView = (SearchView) searchItem.getActionView();
To
searchView = (SearchView) searchItem.getActionView();
After that, at your onDialogPositiveClick()
. You refer it.
@Override
public void onDialogPositiveClick(FiltroTemasDialogFragment dialog) {
// ...
buscador(searchView.getQuery());
}