Searchview with Arraylist Android Studio

Asked

Viewed 22 times

0

Good night, I’m a beginner and I’m having trouble creating a Searchview with Arraylist. I have tried several examples or manuals on the internet but without success, I still cannot understand how the Filter is made in onQueryTextChange. I have a study app where I have a list of films in Firebase with Title, Genre and Year, where I need to search in the searchVIew the title of the films and bring me what was researched Follows Information.

Tela App

Mainactivity.class

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private List<Filme> listaFilmes = new ArrayList<>();

private Adapter adapter;
private DatabaseReference referencia;

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

    referencia = FirebaseDatabase.getInstance().getReference();
    recyclerView = findViewById(R.id.recyclerView);

    //Lista de Filmes
    this.criarFilmes();

    //Configurar RecyclerView
    adapter = new Adapter(listaFilmes);

    //Configurar RecyclerView
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
    recyclerView.setAdapter(adapter);
}

private void criarFilmes() {

    DatabaseReference dados = referencia.child("Filmes");

    dados.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot valores: snapshot.getChildren()){
                Filme filme = valores.getValue(Filme.class);
                listaFilmes.add(filme);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem item = menu.findItem((R.id.action_sarch));
    SearchView searchView = (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            String text = newText;


            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

}

Adapter.class

public class Adapter extends RecyclerView.Adapter<Adapter.MyviewHolder> {


private List<Filme> listaFilmes = null;
private ArrayList<Filme> arrayList;


public Adapter(List<Filme> listaFilmes) {
    this.listaFilmes = listaFilmes;
    this.arrayList = new ArrayList<Filme>();
    this.arrayList.addAll(listaFilmes);
}

@NonNull
@Override
public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemLista = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false);
    return new MyviewHolder(itemLista);
}

@Override
public void onBindViewHolder(@NonNull MyviewHolder holder, int position) {
    Filme filme = listaFilmes.get(position);
    holder.titulo.setText(filme.getTitulo());
    holder.genero.setText(filme.getGenero());
    holder.ano.setText(filme.getAno());
}

@Override
public int getItemCount() {
    return listaFilmes.size();
}


public class MyviewHolder extends RecyclerView.ViewHolder{

    TextView titulo, genero, ano;

    public MyviewHolder(@NonNull View itemView) {
        super(itemView);

        titulo = itemView.findViewById(R.id.textTitulo);
        genero = itemView.findViewById(R.id.textGenero);
        ano = itemView.findViewById(R.id.textAno);

    }
}

}

Film.class

public class Filme {

private String titulo, genero, ano;

public Filme() {
}

public Filme(String titulo, String genero, String ano) {
    this.titulo = titulo;
    this.genero = genero;
    this.ano = ano;
}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getGenero() {
    return genero;
}

public void setGenero(String genero) {
    this.genero = genero;
}

public String getAno() {
    return ano;
}

public void setAno(String ano) {
    this.ano = ano;
}

}

I’m waiting for help...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.