Cant Firebase 在单个 RecyclerView 中显示来自多个引用的数据

Posted

技术标签:

【中文标题】Cant Firebase 在单个 RecyclerView 中显示来自多个引用的数据【英文标题】:Cant Firebase display data from multiple references in a single RecyclerView 【发布时间】:2021-11-09 05:58:58 【问题描述】:
      //This is my fragment where i want to print the data. 
 public class GestaoMarcacaoClienteFragment extends Fragment 
    
        private RecyclerView recyclerMarcacaoCliente;
        private Button botaoAddmarcacao;
        private GestaoMarcacoesClienteAdapter gestaoMarcacoesClienteAdapter;
        private List<Marcacao> marcacaoList = new ArrayList<>();
        private List<Servicos> servicosList = new ArrayList<>();
        private FirebaseAuth auth;
        private DatabaseReference marcacoesRef, servicosRef;
    
        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";
    
        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;
    
        public GestaoMarcacaoClienteFragment() 
            // Required empty public constructor
        
    
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment GestaoMarcacaoClienteFragment.
         */
        // TODO: Rename and change types and number of parameters
        public static GestaoMarcacaoClienteFragment newInstance(String param1, String param2) 
            GestaoMarcacaoClienteFragment fragment = new GestaoMarcacaoClienteFragment();
            Bundle args = new Bundle();
            args.putString(ARG_PARAM1, param1);
            args.putString(ARG_PARAM2, param2);
            fragment.setArguments(args);
            return fragment;
        
    
        @Override
        public void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            if (getArguments() != null) 
                mParam1 = getArguments().getString(ARG_PARAM1);
                mParam2 = getArguments().getString(ARG_PARAM2);
            
        
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_gestao_marcacao_cliente, container, false);
    
            auth = ConfiguracaoFirebase.getAuth();
            botaoAddmarcacao = view.findViewById(R.id.botaoAddMarc);
            recyclerMarcacaoCliente = view.findViewById(R.id.recyclerMarcacaoCliente);
    
            marcacoesRef = ConfiguracaoFirebase.getReference();
            servicosRef = ConfiguracaoFirebase.getReference();
    
            //apanhar dados marcacoes
            String emailUser = auth.getCurrentUser().getEmail();
            String emailEnc = Base64Custom.codificarBase64(emailUser);
            String data = DateCustom.dataFormatada(DateCustom.dataAtual());
            marcacoesRef.child("marcacao").orderByChild("idUtilizador").equalTo(emailEnc).addValueEventListener(new ValueEventListener() 
                @Override
                public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) 
                    marcacaoList.clear();
                    for (DataSnapshot dados : snapshot.getChildren()) 
                        for (DataSnapshot b : dados.getChildren()) 
                            for(DataSnapshot d : b.getChildren())
                                Marcacao m = d.getValue(Marcacao.class);
                                String id = m.getIdServico();
                                Log.i("Dados", " Dados: " + dados);
                                Log.i("Dados", " Dados: " + m.getData());
                                Log.i("Dados", " Dados: " + m.getHora());
                                Log.i("Dados", " Id: " + id);
                                marcacaoList.add(m);
    
                                servicosRef.child("servicos").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() 
                                    @Override
                                    public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) 
                                        servicosList.clear();
                                        for (DataSnapshot d : snapshot.getChildren()) 
                                            Servicos servicos = d.getValue(Servicos.class);
                                            servicosList.add(servicos);
                                        
                                    
    
                                    @Override
                                    public void onCancelled(@NonNull @NotNull DatabaseError error) 
                                        Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                                    
                                );
    
                            
                        
                    
    
                    gestaoMarcacoesClienteAdapter.notifyDataSetChanged();
                
    
                @Override
                public void onCancelled(@NonNull @NotNull DatabaseError error) 
                    Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                
            );
    
    
            //Definir layout recycle view
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
            recyclerMarcacaoCliente.setLayoutManager(layoutManager);
            recyclerMarcacaoCliente.setHasFixedSize(true);
            recyclerMarcacaoCliente.addItemDecoration(new SimpleDividerItemDecoration(getContext()));
    
            //definir adapter
            gestaoMarcacoesClienteAdapter = new GestaoMarcacoesClienteAdapter(marcacaoList, servicosList, getContext());
            recyclerMarcacaoCliente.setAdapter(gestaoMarcacoesClienteAdapter);
    
            botaoAddmarcacao.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    AddMarcacaoFragment firstFragment = new AddMarcacaoFragment();
                    getActivity().getSupportFragmentManager().beginTransaction()
                            .replace(R.id.frameGestaoMarcacoesCliente, firstFragment).commit();
                
            );
    
    
            return view;
        
    
    
    

这是我的实时数据库,其中“idUtilizador”与“usuarios”节点相同,“idServico”是“Servicos”中的 id

我的火力基地规则 This are my rules from firebae

然后这是我的适配器,我想在上面打印什么,我想从 servicos 打印“preco”和“nomeServico”,从“marcacoes”打印“data”和“hora”,但我的回收器上没有任何内容我的 logcat 和运行窗口上什么也没有出现...为什么会这样???索莫帮忙?

【问题讨论】:

【参考方案1】:

FirebaseUI 中没有内置任何东西来显示来自这种数据结构的数据。您必须创建自己的适配器,或者您可以重载populateView 并从其中的附加节点加载数据,或者您可以实现自己的SnapshotParser。后者没有很好的文档记录,但请查看thread 以开始使用。

【讨论】:

我已经读过了,所以我应该将我的适配器更改为 FirebaseRecyclerView 适配器以使其更容易吗?我知道我需要更改我的 BindView Holder,但我仍然不知道如何。我是 android Studio 的新手,我坚持了 5 天...非常感谢您的帮助

以上是关于Cant Firebase 在单个 RecyclerView 中显示来自多个引用的数据的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 在单个请求中发送多个通知

如何在 Firebase 中将多个 Recyclerview 设置为单个适配器(多视图类型)?

Firebase 在子循环中检索单个值

Firebase身份验证登录必须允许单个设备登录

如何从 Firebase 获取单个数据库引用

如何在单个 Firebase 数据库中为多个应用构建推送令牌