Good afternoon! I am trying to implement the search option within a list of songs that I have inside the app, I did some searches on the internet but I still could not get the search to return results in the list, with the code below, when entering the text in the icon of search the list does not return anything, the results add up, I do not know how to create the search algorithm if it should be created in the adapter or inside the main class.
RepertoireActivity (Displays the list of songs for the user and contains the searchview as a menu)
public class RepertorioActivity extends AppCompatActivity {
//ATRIBUTOS
private ListView listView;
private ArrayList<Hinos> arrayList;
private ArrayAdapter adapter;
private DatabaseReference reference;
private ValueEventListener valueEventListener;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_repertorio);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//DEFININDO A TRANSIÇÃO DE ENTRADA DA ACTIVITY
if (Build.VERSION.SDK_INT >= 22){
Slide s = new Slide();
s.setDuration(750);
getWindow().setEnterTransition(s);
}
//Montando a lista de exibição dos hinos
listView = findViewById(R.id.lv_hinos);
arrayList = new ArrayList<>();
adapter = new HinosAdapter(RepertorioActivity.this, arrayList);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
//*************** FIREBASE INICIO ************************/
reference = ConfiguracaoFirebase.getFirebaseReference().child("MOCIDADE")
.child("HINOS");
reference.orderByKey();
valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
arrayList.clear();
for (DataSnapshot data: dataSnapshot.getChildren()){
Hinos hinos = data.getValue(Hinos.class);
arrayList.add(hinos);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
//*************** FIREBASE FIM ************************/
/**************** COMPONENTES DA TELA ********************/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
Hinos hinos = arrayList.get(position);
//salvando no shared para ser utilizado depois
SharedPreferencias preferences = new SharedPreferencias(RepertorioActivity.this);
preferences.salvarHinoPreferences(hinos.getNumero(), hinos.getTitulo(), hinos.getCantor(), hinos.getUrl());
Intent letraA = new Intent(RepertorioActivity.this, MusicaActivity.class);
startActivity(letraA);
}
});
}
/******************************* METODOS *********************************/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mn_add_hino:
if (Atalhos.verificarRegente(getApplicationContext()) == true) {
AdicionarHinoFragment hinoFragment = new AdicionarHinoFragment();
hinoFragment.show(RepertorioActivity.this.getSupportFragmentManager(), "Alert Adicionar Hino");
} else {
Atalhos.acessoNegado(RepertorioActivity.this);
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mocidade, menu);
MenuItem searchItem = menu.findItem(R.id.mn_pesquisar_hino);
//Filtrando os elementos
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
sv.clearFocus();
if (query.toString() != null && !query.toString().equals("")){
adapter.getFilter().filter(query);
} else {
Toast.makeText(RepertorioActivity.this, "Nada encontrado", Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
//return true;
}
@Override
protected void onStart() {
super.onStart();
reference.addValueEventListener(valueEventListener);
}
@Override
protected void onStop() {
super.onStop();
reference.removeEventListener(valueEventListener);
}
}
Adapter for the list of songs
public class HinosAdapter extends ArrayAdapter<Hinos>{
//ATRIBUTOS
Context context;
private ArrayList<Hinos> arrayList;
public HinosAdapter(@NonNull Context c, ArrayList<Hinos> objects) {
super(c, 0, objects);
this.context = c;
this.arrayList = objects;
getFilter();
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = null;
SharedPreferencias preferencias = new SharedPreferencias(context);
//Validando e criando a lista de hinos
if (arrayList != null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//montando a view a partir do XML
view = inflater.inflate(R.layout.lista_hinos, parent, false);
//Recuperando os elementos para exibição
TextView tvNumero = view.findViewById(R.id.tv_num_hino);
TextView tvTitulo = view.findViewById(R.id.tv_titulo_hino);
TextView tvCantor = view.findViewById(R.id.tv_cantor_hino);
final ImageView imLike = view.findViewById(R.id.img_like_lista_hinos);
final Hinos hinos = arrayList.get(position);
tvNumero.setText(hinos.getNumero());
tvCantor.setText(hinos.getCantor());
tvTitulo.setText(hinos.getTitulo());
Curtidas.verificarCurtida(hinos.getNumero(), preferencias.getCHAVE_ID(), imLike, null,"Hinos");
imLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Curtidas.favoritar(context, imLike, null, hinos.getNumero(), 3);
}
});
}
return view;
}
}
Follow activity print for better idea illustration