SearchView MenuItem on Android

0

I'm trying to implement the search function inside a ListView in my app, but I'm missing some connection with the adapter that I can not figure out. With the code below, when typing in the search item no item is found, there is probably something missing from the adapter but I do not know what it can be. menu:

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/mn_pesquisar_hino"
    android:icon="@drawable/ic_search"
    android:title="Search"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="ifRoom" />

<item android:id="@+id/mn_add_hino"
    android:title="Adicionar Hinos"
    android:icon="@drawable/ic_playlist_add"
    app:showAsAction="ifRoom"/>

</menu>

Activity that receives the menu:

public class RepertoryActivity extends AppCompatActivity {     // ATTRIBUTES     private ListView listView;     private ArrayList 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 onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_mocidade, menu);
    MenuItem searchItem = menu.findItem(R.id.mn_pesquisar_hino);

    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();
            }*/

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);
            return false;
        }
    });

    return super.onCreateOptionsMenu(menu);
    //return true;
}

@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);
}

and the Adapter:

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;
}

}

To be more exact I would like to do the research according to the existing text in tvTitulo.

    
asked by anonymous 11.06.2018 / 14:10

0 answers