提问者:小点点

从Firebase中的列表中检索数据


如何从列表中的项目在回收视图中显示数据。 我是说,在node中有很多帖子,但我只想展示其中的一些,这些值在一个列表中。 我怎么能那么做?

方法填充列表

//Pegando os dados da tabela de referência
    mRef = FirebaseDatabase.getInstance().getReference( "Usuarios" ).child( uid ).child( "Favoritos" );
    mRef.addListenerForSingleValueEvent( new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                ds.clear();
                for (DataSnapshot dss : dataSnapshot.getChildren()) {
                    String posts = dss.getValue( String.class );
                    ds.add( posts ); //this is the list
                }
                
            }
        }

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

        }
    } );

显示数据的方法

recyclerView = findViewById( R.id.recyclerFavoritos );
    recyclerView.setHasFixedSize( true );

    layoutDeCarregamento = new LinearLayoutManager( this );
    layoutDeCarregamento.setReverseLayout( true );
    layoutDeCarregamento.setStackFromEnd( true );

    //Definindo o 'Layout'
    recyclerView.setLayoutManager( layoutDeCarregamento );

    mRef = FirebaseDatabase.getInstance().getReference( "Publicacoes" ).child( favoritos );
    FirebaseRecyclerAdapter<Noticias, dados_noticias> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Noticias, dados_noticias>(
                    Noticias.class,
                    R.layout.card_noticias,
                    dados_noticias.class,
                    mRef
            ) {
                @Override
                //Método para fazer o preenchimento dos dados na 'Recycler View'
                protected void populateViewHolder(dados_noticias viewHolder, Noticias noticias, int i) {

                    viewHolder.setDetails( getBaseContext(), noticias.getTitulo(), noticias.getImagem_titulo(), noticias.getVisualizacoes(), noticias.getData(), noticias.getConteudo(), noticias.getPos() );
                }

                @Override
                public dados_noticias onCreateViewHolder(final ViewGroup parent, int viewType) {
                    final dados_noticias viewHolder = super.onCreateViewHolder( parent, viewType );
                    viewHolder.setOnClickListener( new dados_noticias.ClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {


                        }

                        @Override
                        public void onItemLongClick(View view, int position) {
                            Toast.makeText( getApplicationContext(), "Ooops, erro aqui!", Toast.LENGTH_SHORT ).show();
                        }
                    } );
                    return viewHolder;
                }

共1个答案

匿名用户

来自FirebaseUI库的FireBaseRecyclerAdapter类只能通过直接从数据库本身加载数据来显示数据,因此当您向它传递一个DatabaseReferenceQuery对象时。 它没有显示您已经自己加载的数据的选项。

幸运的是,Android中有内置的适配器,这意味着您只需对recyverView.adapter进行子类化即可实现这些方法(类似于您现在使用FireBaseRecyclerAdapter所做的工作)。

有关完整的示例,请参阅Android文档中关于向回收器视图添加列表适配器的内容,以及简单的Android回收器视图示例。