【问题标题】:Firebase favorite user listFirebase 收藏用户列表
【发布时间】:2020-07-11 11:47:21
【问题描述】:

我有一个包含 Users>userUID>Favorites 的 Firebase 数据库,在这个“Favorite”节点中,我有一些用户保存到您最喜欢的帖子的列表,但我想在 RecyclerView 中显示一个仅包含帖子的列表已保存,并且帖子在另一个节点帖子>...我该怎么做,我的意思是,仅获取已保存的帖子并从另一个节点搜索以显示在 RecyclerView 中??

这是我用来添加到用户收藏列表的代码

private void favoritos() {

    final DatabaseReference ref = FirebaseDatabase.getInstance().getReference( "Usuarios" );
    ref.child( mAuth.getUid() ).child( "Favoritos" ).child( posicao )
            .addListenerForSingleValueEvent( new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {
                        if (dataSnapshot.getKey().equals( posicao )) {
                            botaoFavorito.setImageResource( R.drawable.ic_favoritos );
                            final DatabaseReference ref = FirebaseDatabase.getInstance().getReference( "Usuarios" );
                            ref.child( mAuth.getUid() ).child( "Favoritos" ).child( posicao ).setValue( null );
                        }
                    } else {
                        botaoFavorito.setImageResource( R.drawable.ic_favorito_adicionado );
                        final DatabaseReference ref = FirebaseDatabase.getInstance().getReference( "Usuarios" );
                        ref.child( mAuth.getUid() ).child( "Favoritos" ).child( posicao ).setValue( posicao );
                    }
                }

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

                }
            } );
        }

这就是我的 RecyclerView 显示数据的方式

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

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

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

firebaseDatabase = FirebaseDatabase.getInstance();

//Pegando os dados da tabela de referência
mRef = firebaseDatabase.getReference( "Usuarios" );
mRef.child( uid ).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;
            }
        };

//Mandando o adapter para o 'Recycler View'
recyclerView.setAdapter( firebaseRecyclerAdapter );

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    如果你以前做过,但以另一种方式,我希望它对你有所帮助:

    首先,我没有将我的收藏夹保存在 Firebase 上,而是以 GSON 的形式将它们保存在缓存中,这是我认为的一个重要原因,并且没有任何好处每次他必须打开应用程序以从 Firebase 下载收藏夹时,您以后不会只对一个用户进行测量

    第二个:我把保存的数据作为List取出来发送到Adapter

    如果你想使用这个方法,代码如下:

    在类实用程序中:

    public static List<String> getFavIDsList(Context context) {
            return Util.loadArrayFromPreference(context, Constant.FAV_ID_PREF);
        }
    
    
    public static List<Product> getFavProductList(Context context) {
    
        List<Product> productList = new ArrayList<>();
    
        List<String> gsonList = Util.loadArrayFromPreference(context, Constant.FAV_PRODUCT_PREF);
    
        for (String gson : gsonList)
            productList.add(new Gson().fromJson(gson, Product.class));
    
        return productList;
    }
    
    
    
    public static void updateProductListFav(Context context, Product product, boolean add) {
    
        //save for id list
        List<String> productIdList = getFavIDsList(context);
        if (add)
            productIdList.add(product.getId());
        else
            productIdList.remove(product.getId());
    
        Util.saveArrayToPreference(context, Constant.FAV_ID_PREF, productIdList);
    
        //save for object json list
        List<String> gsonProductList = Util.loadArrayFromPreference(context, Constant.FAV_PRODUCT_PREF);
        if (add)
            gsonProductList.add(new Gson().toJson(product));
        else{
            for(int i = 0 ; i< gsonProductList.size() ; i++){
                if(new Gson().fromJson(gsonProductList.get(i),Product.class).getId().equals(product.getId())){
                    gsonProductList.remove(i);
                    break;
                }
            }
        }
    
    
        Util.saveArrayToPreference(context, Constant.FAV_PRODUCT_PREF, gsonProductList);
    
    }
    

    FavoritesActivity

    productAdapter = new ProductAdapter(getContext());
    
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    
            binding.favoriteRv.setLayoutManager(layoutManager);
    
            binding.favoriteRv.setAdapter(productAdapter);
    
            productAdapter.setProductList(ProductUtil.getFavProductList(getContext()));
    

    在 Adpater 中:

    public void setProductList(List<Product> productList) {
    
            this.productList = productList;
            notifyDataSetChanged();
        }
    
      
    

    我希望我帮助您解决了这个问题。

    【讨论】:

    • 等待 ksksk 你说先保存在 GSON 中,然后再保存到 Firebase 中??我的用户节点是User>userID>Favorites>,这里是保存的所有帖子,帖子的节点是Posts>,这里是所有帖子。我认为的方式是获取用户节点中的所有帖子,然后在帖子节点中关联以仅显示用户保存的内容,但我不知道如何进行此比较。事实上,我是一名土木工程专业的学生,​​试图制作一个应用程序来帮助我们,我的代码级别只是基本的 kskskksksk 你知道如何进行比较吗?非常感谢!!!
    猜你喜欢
    • 2018-06-12
    • 2018-09-04
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2017-02-11
    • 2020-05-28
    相关资源
    最近更新 更多