显示的信息重复android工作室,RecyclerView适配器notifyDataSetChanged不工作



首先,很抱歉英语不好,我会说西班牙语,并使用谷歌翻译。

问题是,每次我进入查看投诉时,它都会完美加载,但当我返回并再次输入时,信息会重复。

首次准入

关于第二次准入

这是适配器

package com.example.denuncia.adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.denuncia.R;
import com.example.denuncia.model.Denuncia;
import java.util.List;
public class DenunciaAdapter extends RecyclerView.Adapter<DenunciaAdapter.DenunciaHolder> {
List<Denuncia> list;
int layout;
Activity activity;
public DenunciaAdapter(List<Denuncia> list, int layaut, Activity activity) {
this.list = list;
this.layout = layaut;
this.activity = activity;
}
@NonNull
@Override
public DenunciaHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(layout,parent,false);
return new DenunciaHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DenunciaAdapter.DenunciaHolder holder, int position) {
Denuncia denuncia = list.get(position);
holder.txttipo.setText(denuncia.getTipo_denuncia());
holder.txtdireccion.setText(denuncia.getDireccion());
holder.txtnombre.setText(denuncia.getTitulo_denuncia());
holder.txtestado.setText(denuncia.getEstado_denuncia());
Glide.with(activity).load(denuncia.getUrl()).into(holder.imagen);
}
@Override
public int getItemCount() {
return list.size();
}
public class DenunciaHolder extends RecyclerView.ViewHolder{
TextView txtnombre,txtdireccion,txtestado,txttipo;
ImageView imagen;
public DenunciaHolder(@NonNull View itemView) {
super(itemView);
txttipo = itemView.findViewById(R.id.tipo);
txtdireccion = itemView.findViewById(R.id.direccionDenuncia);
txtestado = itemView.findViewById(R.id.estadoDenuncia);
txtnombre = itemView.findViewById(R.id.nombreDenuncia);
imagen = itemView.findViewById(R.id.img_item);
}
}
}

这是我的服务

package com.example.denuncia.adapter;
import com.example.denuncia.model.Denuncia;
import java.util.ArrayList;
import java.util.List;
public class DenunciaService {
public static List<Denuncia> denuncias=new ArrayList<>();
public static void addDenuncia(Denuncia denuncia){
denuncias.add(denuncia);
}
public static void updateDenuncia(Denuncia denuncia){
denuncias.set(denuncias.indexOf(denuncia),denuncia);
}
}

这是类

package com.example.denuncia;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.example.denuncia.adapter.DenunciaAdapter;
import com.example.denuncia.adapter.DenunciaService;
import com.example.denuncia.model.Denuncia;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class ver_denuncias extends AppCompatActivity {
RecyclerView rc;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_denuncias);
firebaseAuth        = FirebaseAuth.getInstance();
rc = findViewById(R.id.rc);
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setOrientation(RecyclerView.VERTICAL);
rc.setLayoutManager(lm);
DenunciaAdapter adapter = new DenunciaAdapter(DenunciaService.denuncias,R.layout.item,this);
rc.setAdapter(adapter);
cargaDatosFirebase();
}

public void cargaDatosFirebase(){
FirebaseDatabase database = FirebaseDatabase.getInstance();
String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference myRef = database.getReference("Denuncias").child(uid);
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Denuncia denuncia = snapshot.getValue(Denuncia.class);
if (!DenunciaService.denuncias.contains(denuncia)) {
DenunciaService.addDenuncia(denuncia);
}
rc.getAdapter().notifyDataSetChanged();
}

@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Denuncia denuncia = snapshot.getValue(Denuncia.class);
if (DenunciaService.denuncias.contains(denuncia)) {
DenunciaService.updateDenuncia(denuncia);
}
rc.getAdapter().notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}

public void perfil(View view) {
Intent intent = new Intent(ver_denuncias.this, perfil_Activity.class);
startActivity(intent);
finish();
}

我认为问题在于您重复调用DenunciaService.addDenuncia(denuncia);,并且当您返回到执行onCreate方法时,您的列表包含以前的项目。

所以,你有两个选择:

  1. 在调用cargaDatosFirebase之前,检查您的denunciasList是否为空

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //(...)
    //your current code here
    //check if list is empty before load data
    if (DenunciaService.denuncias.size() == 0)
    cargaDatosFirebase();
    }
    
  2. 在呼叫cargaDatosFirebase之前,尝试clear您的denunciasList

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //(...)
    //your current code here
    //clear list before load data
    DenunciaService.denuncias.clear();
    cargaDatosFirebase();
    }
    

最新更新