通过dataSnapshot.getKey()咨询来自2个不同节点的数据

时间:2019-07-08 06:13:13

标签: android firebase firebase-realtime-database android-recyclerview recycler-adapter

我正在使用Firebase,并且正在尝试将Recyclerview与适配器集成在一起,我正在经历一个令人困惑的时刻,因为我需要通过getKey来查询两个不同的节点并获取并带来来自两个节点的信息,以获取信息并与它们一起使用。

我已经做了一些小测试,但是在执行查询时,我只获得了.getKey,但是值附带的所有内容都为null,并且不获取数据。

enter image description here

public class ActiveAchievements {

    private String descripcion;
    private String pageLink;
    private String photoUrl;
    private int qaploins;
    private String tiempoLimite;
    private String tipoLogro;
    private String titulo;
    private int totalPuntos;
    private String idLogro;
    private int puntosCompletados;
    private boolean redimido;


    public ActiveAchievements(String descripcion, String pageLink, String photoUrl, int qaploins, String tiempoLimite, String tipoLogro, String titulo, int totalPuntos, String idLogro, int puntosCompletados, boolean redimido) {
        this.descripcion = descripcion;
        this.pageLink = pageLink;
        this.photoUrl = photoUrl;
        this.qaploins = qaploins;
        this.tiempoLimite = tiempoLimite;
        this.tipoLogro = tipoLogro;
        this.titulo = titulo;
        this.totalPuntos = totalPuntos;
        this.idLogro = idLogro;
        this.puntosCompletados = puntosCompletados;
        this.redimido = redimido;
    }

    public ActiveAchievements() {

    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getPageLink() {
        return pageLink;
    }

    public void setPageLink(String pageLink) {
        this.pageLink = pageLink;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public int getQaploins() {
        return qaploins;
    }

    public void setQaploins(int qaploins) {
        this.qaploins = qaploins;
    }

    public String getTiempoLimite() {
        return tiempoLimite;
    }

    public void setTiempoLimite(String tiempoLimite) {
        this.tiempoLimite = tiempoLimite;
    }

    public String getTipoLogro() {
        return tipoLogro;
    }

    public void setTipoLogro(String tipoLogro) {
        this.tipoLogro = tipoLogro;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public int getTotalPuntos() {
        return totalPuntos;
    }

    public void setTotalPuntos(int totalPuntos) {
        this.totalPuntos = totalPuntos;
    }

    public String getIdLogro() {
        return idLogro;
    }

    public void setIdLogro(String idLogro) {
        this.idLogro = idLogro;
    }

    public int getPuntosCompletados() {
        return puntosCompletados;
    }

    public void setPuntosCompletados(int puntosCompletados) {
        this.puntosCompletados = puntosCompletados;
    }

    public boolean isRedimido() {
        return redimido;
    }

    public void setRedimido(boolean redimido) {
        this.redimido = redimido;
    }
}

public class AssetsAdapterQ extends RecyclerView.Adapter<AssetsAdapterQ.ViewHolder> {


    Context context;
    List<ActiveAchievements> categorieslist;
    String myUserUid;
    /** FIREBASE **/
    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mUsersDatabaseReference;
    private DatabaseReference mLogrosDatabaseReference;
    private DatabaseReference mlogroIncompletoDatabaseReference;



    public AssetsAdapterQ(Context context, List<ActiveAchievements> contactList) {
        this.context = context;
        this.categorieslist = contactList;
    }


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

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_view_assents, parent, false);

        return new ViewHolder(itemView);

    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, final int position) {
        final ActiveAchievements achievements = categorieslist.get(position);

        Viewholder.title.setText(achievements.getTitulo());
        Viewholder.descripcion.setText(achievements.getDescripcion());
        Viewholder.conins.setText(achievements.getQaploins()+"");
        Viewholder.timer.setText(achievements.getTiempoLimite());
        Viewholder.progresstext.setText(achievements.getTotalPuntos()+"");
        Viewholder.points.setText("0");

        /**
        Glide.with(context)
                .load(categoriesP.getLogo())
                .apply(new RequestOptions().override(200, 200))
                .error(Glide.with(context).load(R.mipmap.ic_launcher))
                .into(Viewholder.imgLogo);**/


    }

    @Override
    public int getItemCount() {

        return categorieslist.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imgLogo;
        public TextView title,conins,descripcion,timer,points,progresstext;
        public ProgressBar progress;




        public ViewHolder(View itemView) {

            super(itemView);

            //imgLogo = itemView.findViewById(R.id.imgLogo);
            title = itemView.findViewById(R.id.title_text);
            conins = itemView.findViewById(R.id.conins_text);
            descripcion = itemView.findViewById(R.id.info_text);
            timer = itemView.findViewById(R.id.timer_text);
            points = itemView.findViewById(R.id.progress_text);
            points = itemView.findViewById(R.id.progress_text);
            progresstext = itemView.findViewById(R.id.total_progress_text);
            progress = itemView.findViewById(R.id.progress_assets);


        }
    }
}

public class Assets extends Fragment {

    private RecyclerView mRecyclerView, mRecyclerViewS, mRecyclerViewV, mRecyclerViewO;
    private List<ActiveAchievements> list;
    private List<ActiveAchievements> listQ;
    private List<ActiveAchievements> listS;
    private List<ActiveAchievements> listV;
    private List<ActiveAchievements> listO;
    AssetsAdapterQ adapter;

    /**
     * FIREBASE
     **/
    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mLogrosActivosDatabaseReference;
    private DatabaseReference mLogrosDatabaseReference;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.item_recyclerview, container, false);


        /**FIREBASE DATA**/
        firebaseAuth = FirebaseAuth.getInstance();
        mFirebaseDatabase = FirebaseDatabase.getInstance();

        // mRecyclerView QAPLOINS
        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        // mRecyclerView SOCIAL
        mRecyclerViewS = view.findViewById(R.id.recycler_view1);
        mRecyclerViewS.setHasFixedSize(true);
        mRecyclerViewS.setNestedScrollingEnabled(false);
        mRecyclerViewS.setLayoutManager(new LinearLayoutManager(getActivity()));


        // mRecyclerView VERIFICADO
        mRecyclerViewV = view.findViewById(R.id.recycler_view2);
        mRecyclerViewV.setHasFixedSize(true);
        mRecyclerViewV.setNestedScrollingEnabled(false);
        mRecyclerViewV.setLayoutManager(new LinearLayoutManager(getActivity()));


        // mRecyclerView xOVER
        mRecyclerViewO = view.findViewById(R.id.recycler_view3);
        mRecyclerViewO.setHasFixedSize(true);
        mRecyclerViewO.setNestedScrollingEnabled(false);
        mRecyclerViewO.setLayoutManager(new LinearLayoutManager(getActivity()));


        mLogrosActivosDatabaseReference = FirebaseDatabase.getInstance().getReference().child("logrosActivos");
        mLogrosDatabaseReference = mFirebaseDatabase.getReference().child("logros");
        final String userUid = firebaseAuth.getCurrentUser().getUid();


        mLogrosActivosDatabaseReference.keepSynced(true);
        mLogrosActivosDatabaseReference.child("qaploins").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list = new ArrayList<ActiveAchievements>();

                if (dataSnapshot.exists()) {


                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        ActiveAchievements p = dataSnapshot1.getValue(ActiveAchievements.class);
                        ActiveAchievements fire = new ActiveAchievements();

                        String logroId = p.getIdLogro();
                        logroId = dataSnapshot1.getKey();
                        String titulo = p.getTitulo();
                        String descripcion = p.getDescripcion();
                        String pageLink = p.getPageLink();
                        String photoUrl = p.getPhotoUrl();
                        int qaploins = p.getQaploins();
                        String tiempoLimite = p.getTiempoLimite();
                        String tipoLogro = p.getTipoLogro();
                        int totalPuntos = p.getTotalPuntos();

                        mLogrosDatabaseReference.child(userUid).child("logroIncompleto").child(logroId).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                list = new ArrayList<>();

                                if (dataSnapshot.exists()){

                                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                                        ActiveAchievements p = dataSnapshot1.getValue(ActiveAchievements.class);
                                        ActiveAchievements fire = new ActiveAchievements();

                                        int puntoscompletados = p.getPuntosCompletados();
                                        int totalPuntos = p.getTotalPuntos();
                                        boolean redimido = p.isRedimido();
                                        list.add(p);

                                    }
                                }else {

                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                             //   Toast.makeText(Assets.this, "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();

                            }
                        });

                        list.add(p);
                    }

                    adapter = new AssetsAdapterQ(getActivity(), list);
                    mRecyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(), "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
            }
        });

        mLogrosActivosDatabaseReference.child("social").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list = new ArrayList<ActiveAchievements>();

                if (dataSnapshot.exists()) {

                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        ActiveAchievements p = dataSnapshot1.getValue(ActiveAchievements.class);
                        ActiveAchievements fire = new ActiveAchievements();

                        String titulo = p.getTitulo();
                        String descripcion = p.getDescripcion();
                        String pageLink = p.getPageLink();
                        String photoUrl = p.getPhotoUrl();
                        int qaploins = p.getQaploins();
                        String tiempoLimite = p.getTiempoLimite();
                        String tipoLogro = p.getTipoLogro();
                        int totalPuntos = p.getTotalPuntos();
                        list.add(p);

                    }

                    adapter = new AssetsAdapterQ(getActivity(), list);
                    mRecyclerViewS.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(), "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
            }
        });


        mLogrosActivosDatabaseReference.child("verificado").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list = new ArrayList<ActiveAchievements>();

                if (dataSnapshot.exists()) {

                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        ActiveAchievements p = dataSnapshot1.getValue(ActiveAchievements.class);
                        ActiveAchievements fire = new ActiveAchievements();

                        String titulo = p.getTitulo();
                        String descripcion = p.getDescripcion();
                        String pageLink = p.getPageLink();
                        String photoUrl = p.getPhotoUrl();
                        int qaploins = p.getQaploins();
                        String tiempoLimite = p.getTiempoLimite();
                        String tipoLogro = p.getTipoLogro();
                        int totalPuntos = p.getTotalPuntos();
                        list.add(p);

                    }

                    adapter = new AssetsAdapterQ(getActivity(), list);
                    mRecyclerViewV.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(), "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
            }
        });


        mLogrosActivosDatabaseReference.child("xOver").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list = new ArrayList<ActiveAchievements>();

                if (dataSnapshot.exists()) {

                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        ActiveAchievements p = dataSnapshot1.getValue(ActiveAchievements.class);
                        ActiveAchievements fire = new ActiveAchievements();

                        String titulo = p.getTitulo();
                        String descripcion = p.getDescripcion();
                        String pageLink = p.getPageLink();
                        String photoUrl = p.getPhotoUrl();
                        int qaploins = p.getQaploins();
                        String tiempoLimite = p.getTiempoLimite();
                        String tipoLogro = p.getTipoLogro();
                        int totalPuntos = p.getTotalPuntos();
                        list.add(p);

                    }

                    adapter = new AssetsAdapterQ(getActivity(), list);
                    mRecyclerViewO.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(), "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
            }
        });


        return view;
    }


}

只要标识符相同,我们希望带来的是每个节点的总数据。

0 个答案:

没有答案