我有一个活动名称“ CoachAlumnos”,它在firebase数据库中的人员的recyclerView上显示数据,我使用FirebaseDatabase.getInstance().getreference().child(“ Usuario”)。addValueEventListener {.。 }工作正常。 我在此活动上有一个按钮,当我按此按钮时,它将带我到另一个活动名称“ AgregarAlumnos”,它添加了一个新人员,它可以正常工作,但是当完成时,我应该传递到第三个活动,但是在上载数据发送到Firebase,它会自动返回到第一个活动,我认为这是因为第一个活动的事件侦听器。而且我不知道如何停止它,我已经在第一个活动上使用了removeEventListener,但是它不起作用。
我用这个:
@Override
protected void onStart (){
super.onStart();
obtenerdatoscoach ();
obtenerdatosdeportistas();
}
private void obtenerdatosdeportistas() {
basedatos.getReference ().child ("Usuario-Alumno").addValueEventListener(listenerdeportisa = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot objeto : dataSnapshot.getChildren()) {
Usuario deportista = objeto.getValue(Usuario.class);
assert deportista != null;
if (deportista.obtenerCoach().equals (getIntent ().getStringExtra("email"))) {
alumnoslista.add(new AlumnoCard(
deportista.obtenerImageUrl(),
deportista.obtenerNombres() + " " + deportista.obtenerApellidos(),
deportista.obtenerTelefono(),
deportista.obtenerEmail(),
deportista.obtenerCoach(),
deportista.obtenerIdUsuario()));
adapter.notifyDataSetChanged();
}
}
if (alumnoslista.size() == 0) {
AlertDialog.Builder builder = new AlertDialog
.Builder(CoachAlumnos.this, R.style.DialogBasicCustomStyle);
builder.setIcon(R.drawable.icono_agregar_deportista);
builder.setTitle("AGREGUE");
builder.setMessage("NO CUENTA AUN CON DEPORTISTAS, AGREGUE A SU PRIMER DEPORTISTA")
.setPositiveButton("AGREGAR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent nuevo = new Intent(CoachAlumnos.this, AgregarNuevo.class);
nuevo.putExtra("email", getIntent().getStringExtra("email"));
startActivity(nuevo);
}
}).setNegativeButton("AHORA NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressBar.setVisibility(View.GONE);
dialog.dismiss();
}
}).setCancelable(false)
.show();
}
progressBar.setVisibility(View.GONE);
//basedatos.getReference ().child ("Usuario-Coach").removeEventListener(listenerdeportisa); //I also try this
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void obtenerdatoscoach (){
basedatos.getReference ().child ("Usuario-Alumno").addValueEventListener(listenercoach = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot objeto : dataSnapshot.getChildren ()){
Usuario coach = objeto.getValue(Usuario.class);
assert coach != null;
if (coach.obtenerEmail().equals (getIntent ().getStringExtra("email"))){
coachid = coach.obtenerIdUsuario();
nombrecoach = coach.obtenerNombres() + " " + coach.obtenerApellidos();
foto_coach = coach.obtenerImageUrl();
Picasso.with (CoachAlumnos.this).load(foto_coach).into(perfilButton);
progressBar.setVisibility(View.GONE);
break;
}
}
//basedatos.getReference ().child ("Usuario-Alumno").removeEventListener(listenerdeportisa); //I also try this
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
protected void onDestroy (){
super.onDestroy();
if (listenerdeportisa != null)
basedatos.getReference ().child ("Usuario-Alumno").removeEventListener(listenerdeportisa);
if (listenercoach != null)
basedatos.getReference ().child ("Usuario-Coach").removeEventListener(listenercoach);
}