0
Good Night, I have an Activity with Recyclerview and Searchview by clicking on any cell brings me a new attempt with the information of the chosen cell, but when searching a given word returns item of position 185 by clicking on the desired option returns cell information of position 0, follow my code for help, shouldn’t the onclick position be reset and a new count be made? How do I fix it?
Frameworks.java activity.
private RecyclerView recyclerViewEnquadramento;
private List<EnquadramentoModel> listaEnquadramentos = new ArrayList<>();
private AdapterEnquadramento adapter;
private DatabaseReference referencia;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
setContentView(R.layout.activity_enquadramento);
referencia = FirebaseDatabase.getInstance().getReference();
recyclerViewEnquadramento = findViewById(R.id.recyclerViewEnquadramento);
this.criarEnquadramentos();
adapter = new AdapterEnquadramento(listaEnquadramentos);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewEnquadramento.setLayoutManager(layoutManager);
recyclerViewEnquadramento.setHasFixedSize(true);
recyclerViewEnquadramento.setAdapter(adapter);
recyclerViewEnquadramento.addOnItemTouchListener(new RecyclerItemClickListener(getApplication() ,recyclerViewEnquadramento, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getBaseContext(), DetalhesEnquadramentoActivity.class);
intent.putExtra("tituloFirebase", listaEnquadramentos.get(position).getTituloFireBase());
startActivity(intent);
}
@Override
public void onLongItemClick(View view, int position) {
}
}));
}
private void criarEnquadramentos() {
DatabaseReference dados = referencia.child("legislacao").child("Enquadramento");
dados.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot valores: snapshot.getChildren()){
EnquadramentoModel enquadramento = valores.getValue(EnquadramentoModel.class);
listaEnquadramentos.add(enquadramento);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void filter(String text){
List<EnquadramentoModel> filteredList = new ArrayList<>();
for (EnquadramentoModel item : listaEnquadramentos){
if (item.getTipificacao_resumida().toLowerCase().contains((text.toLowerCase()))){
filteredList.add(item);
System.out.println(item);
}
}
adapter.filterList(filteredList);
}
@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) {
filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Framing adapter.java
private List<EnquadramentoModel> listaEnquadramentos = null;
private ArrayList<EnquadramentoModel> arrayList;
private String tituloFirebase;
public AdapterEnquadramento(List<EnquadramentoModel> listaEnquadramentos){
this.listaEnquadramentos = listaEnquadramentos;
this.arrayList = new ArrayList<EnquadramentoModel>();
this.arrayList.addAll(listaEnquadramentos);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemLista = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_enquadramento, parent, false);
return new MyViewHolder(itemLista);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
EnquadramentoModel enquadramentoModel = listaEnquadramentos.get(position);
holder.artigo.setText(enquadramentoModel.getArtigo());
holder.tipificacao_resumida.setText(enquadramentoModel.getTipificacao_resumida());
tituloFirebase = enquadramentoModel.getTituloFireBase();
}
@Override
public int getItemCount() {
return listaEnquadramentos.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView artigo, tipificacao_resumida;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
artigo = itemView.findViewById(R.id.textArtigo);
tipificacao_resumida = itemView.findViewById(R.id.tipificacao_resumida);
}
}
public void filterList(List<EnquadramentoModel> filteredList){
listaEnquadramentos = filteredList;
notifyDataSetChanged();
}
Recycleritemclicklistener
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}