Recycleview item returning the same data for all items when clicked

0

I have a list of addresses in Recycleview and each address has its latitude and longitude coordinates retrieved from the database. But when I click on the list item to start a route from the coordinates always brings the same in all items. What should I do? Here are my codes:

My Adapter:

    public class AdapterSolicitacoes extends RecyclerView.Adapter <AdapterSolicitacoes.MyViewHolder> {

private List<DadosSolicitacao> solicitacoes;
private Context context;

public AdapterSolicitacoes(List<DadosSolicitacao> listaSolicitacoes, Context c) {

    this.solicitacoes = listaSolicitacoes;
    this.context = c;

}

public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView endereco;
        TextView material;
        TextView volume;
        Double latitude, longitude;


        public MyViewHolder(View itemView) {
            super(itemView);

            endereco = itemView.findViewById(R.id.tv_endereco);
            material = itemView.findViewById(R.id.tv_tipoMaterial);
            volume = itemView.findViewById(R.id.tv_volumeMaterial);

        }
    }

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemLista = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_lista_solicitacao, parent, false);

    return new MyViewHolder(itemLista);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    DadosSolicitacao dados = solicitacoes.get(position);

       holder.endereco.setText(dados.getEndereco());
       holder.material.setText(dados.getTipoMaterial());
       holder.volume.setText(dados.getVolumeMaterial());

}

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

}

Fragment where I retrieve the information and step to the recycleview

   public class SolicRecebidaFragment extends Fragment {

private RecyclerView recyclerViewSolicRecebida;
private ArrayList<DadosSolicitacao> listaSolicRecebida = new ArrayList<>();
private AdapterSolicitacoes adapter;
private DatabaseReference refSolicRecebida;
private ValueEventListener valueEventoSolicitacao;
private DadosSolicitacao solicitacao;
private static final String TAG = "COORDENADAS";
private Double latitude, longitude;



public SolicRecebidaFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_solic_recebida, container, false);

    //Config.Inciais
    recyclerViewSolicRecebida = view.findViewById(R.id.recyclerView_ListaSolicitacao);

    //Referencia de recuperação de dados
    refSolicRecebida = ConfiguracaoFirebase.getFirebaseDataBase();

    //Config.Adapter
    adapter = new AdapterSolicitacoes(listaSolicRecebida, getActivity());

    //Config.RecyclerView
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerViewSolicRecebida.setLayoutManager(layoutManager);
    recyclerViewSolicRecebida.setHasFixedSize(true);
    recyclerViewSolicRecebida.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
    recyclerViewSolicRecebida.setAdapter(adapter);

    // Evento de clique do Item da Lista
    eventoClickItemLista();


    return view;

}

@Override //Exibi lista ao carregar o fragment
public void onStart() {
    super.onStart();

    recuperarSolicitacao();
}

@Override // Remove lista ao sair do fragment
public void onStop() {
    super.onStop();
    refSolicRecebida.removeEventListener(valueEventoSolicitacao);
}


public void recuperarSolicitacao() {

    valueEventoSolicitacao = refSolicRecebida.child("solicitacao_coleta").orderByChild("a_SolicAtendida")
            .equalTo("nao").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    listaSolicRecebida.clear();

                    for (DataSnapshot dados : dataSnapshot.getChildren()) {



                      // Log.i("LOG3: FDADOS", dataSnapshot.getValue().toString());
                       latitude = Double.valueOf(dados.child("latitude").getValue().toString());
                       longitude = Double.valueOf(dados.child("longitude").getValue().toString());



                        solicitacao = dados.getValue(DadosSolicitacao.class);
                        listaSolicRecebida.add(solicitacao);

                    }

                    adapter.notifyDataSetChanged();

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

}

Click event

    public void eventoClickItemLista() {

    //Evento de click
    recyclerViewSolicRecebida.addOnItemTouchListener(
            new RecyclerItemClickListener(getActivity(), recyclerViewSolicRecebida,
                    new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {


                           //AlertDialog
    android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(getActivity());

    // Titulo do dialogo
    alertDialog.setTitle("Iniciar rota para coleta:");

    // Mensagem do dialogo
    alertDialog.setMessage("Ir para GPS?");

    // botao ir para GPS
    alertDialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {


            //Passar Latitude e Longitude e chamar GPS
            String strUri = "http://maps.google.com/maps?q=loc:" +
                    latitude + "," + longitude;

            Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(strUri));
            intent.setClassName("com.google.android.apps.maps",
                    "com.google.android.maps.MapsActivity");

            getActivity().startActivity(intent);
        }
    });

    // botao cancelar
    alertDialog.setNegativeButton("Não", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    // visualizacao do dialogo
    alertDialog.show();


                        }

                        @Override
                        public void onLongItemClick(View view, int position) {





                        }

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                        }
                    })
    );

}
    
asked by anonymous 06.05.2018 / 10:30

1 answer

3

To add a click event to each separate item or you do here:

  @Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    DadosSolicitacao dados = solicitacoes.get(position);

       holder.endereco.setText(dados.getEndereco());
       holder.material.setText(dados.getTipoMaterial());
       holder.volume.setText(dados.getVolumeMaterial());

       holder.endereco.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Seu código
        }
    });
}

Or do this class:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    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){}
}

And in your activity you call:

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(Context, 
recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                //Seu código
            }

            @Override
            public void onLongItemClick(View view, int i) {
              //Seu código
            }
        })
    );
    
06.05.2018 / 13:43