【问题标题】:duplication of displayed information android studio, RecyclerView Adapter notifyDataSetChanged not working显示信息重复android studio,RecyclerView Adapter notifyDataSetChanged 不工作
【发布时间】:2021-07-14 20:22:20
【问题描述】:

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

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

first admission

on the second admission

这是适配器

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();

    }

【问题讨论】:

    标签: java android firebase android-recyclerview


    【解决方案1】:

    我认为问题在于您重复调用 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 你的denuncias List

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

    【讨论】:

    • 我该怎么做?
    • 这样清理? link
    • 是的,list.clear() 可以工作,但适配器位置不正确。
    • 查看答案,我已经编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2023-03-10
    • 2021-12-22
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多