从火力基地查询的数据不会显示在recyclerview和grid视图中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从火力基地查询的数据不会显示在recyclerview和grid视图中相关的知识,希望对你有一定的参考价值。

我从Firebase查询数据,并将其添加到horizo​​ntalscrollmodellist和viewalllist中,并传递到主页适配器以在网格布局和水平滚动布局中显示,它可以完美地在主页中显示查询的数据。但是,如果我从适配器传递相同的列表以查看所有活动以在回收器视图中显示,并且网格视图代码看起来很好并且没有错误,但是当应用程序运行该视图时,所有活动都不会在网格视图和回收器视图中显示数据。我已经尝试了一切,但问题仍然存在。请帮助我。

谢谢大家!

这是查询列表:

package com.my.electroshop;

import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

public class DBqueries 

    public static  FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    public static List<CatagoryModel> catagoryModelList = new ArrayList<>();
    public static List<HomePageModel> homePageModelList = new ArrayList<>();

    public static void  loadCategories(final CatagoryAdaptor catagoryAdaptor , final Context context)
        firebaseFirestore.collection("CATEGORIES").orderBy("index").get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() 
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) 
                        if (task.isSuccessful())
                            for(QueryDocumentSnapshot documentSnapshot : task.getResult())
                                catagoryModelList.add(new CatagoryModel(documentSnapshot.get("icon").toString(),documentSnapshot.get("categoryName").toString()));
                            
                            catagoryAdaptor.notifyDataSetChanged();
                        else
                            String error = task.getException().getMessage();
                            Toast.makeText(context, error,Toast.LENGTH_SHORT).show();
                        
                    
                );
    

    public static  void loadFragmentData(final HomePageAdapter adapter , final Context context)

        firebaseFirestore.collection("CATEGORIES").document("HOME").collection("TOP_DEALS").orderBy("index").get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() 
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) 
                        if (task.isSuccessful())
                            for(QueryDocumentSnapshot documentSnapshot : task.getResult()) 
                                if ((long) documentSnapshot.get("view_type") == 0) 
                                    List<SliderModel> sliderModelList = new ArrayList<>();
                                    long no_of_banners = (long) documentSnapshot.get("no_of_banners");
                                    for (long x = 1;x < no_of_banners +1;x++) 
                                        sliderModelList.add(new SliderModel(documentSnapshot.get("banner_" + x).toString()
                                                , documentSnapshot.get("banner_" + x + "_background").toString()));
                                    
                                    homePageModelList.add(new HomePageModel(0, sliderModelList));

                                 else if ((long) documentSnapshot.get("view_type") == 1) 
                                    homePageModelList.add(new HomePageModel(1, documentSnapshot.get("strip_ad_banner").toString(),
                                            documentSnapshot.get("background").toString()));

                                 else if ((long) documentSnapshot.get("view_type") == 2) 
                                    List<WishListModel> viewAllProductList = new ArrayList<>();
                                    List<HorizontalProductScrollModel> horizontalProductScrollModelList = new ArrayList<>();
                                    long no_of_products = (long) documentSnapshot.get("no_of_products");
                                    for (long x = 1;x < no_of_products +1;x++) 
                                        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(documentSnapshot.get("product_ID_"+x).toString()
                                                , documentSnapshot.get("product_image_"+x).toString()
                                                ,documentSnapshot.get("product_title_"+x).toString()
                                                ,documentSnapshot.get("product_subtitle_"+x).toString()
                                                ,documentSnapshot.get("product_price_"+x).toString()));
                                        viewAllProductList.add(new WishListModel(documentSnapshot.get("product_image_"+x).toString()
                                                ,documentSnapshot.get("product_full_title_"+x).toString()
                                                ,(long)documentSnapshot.get("free_coupens_"+x)
                                                ,documentSnapshot.get("average_rating_"+x).toString()
                                                ,(long)documentSnapshot.get("total_rating_"+x)
                                                ,documentSnapshot.get("product_price_"+x).toString()
                                                ,documentSnapshot.get("cutted_price_"+x).toString()
                                                ,(boolean)documentSnapshot.get("COD_"+x)));
                                    
                                    homePageModelList.add(new HomePageModel(2, documentSnapshot.get("layout_title").toString()
                                            , documentSnapshot.get("layout_background").toString(), horizontalProductScrollModelList , viewAllProductList));

                                 else if ((long) documentSnapshot.get("view_type") == 3) 

                                    List<HorizontalProductScrollModel> gridLayoutModelList = new ArrayList<>();
                                    long no_of_products = (long) documentSnapshot.get("no_of_products");
                                    for (long x = 1;x < no_of_products +1;x++) 
                                        gridLayoutModelList.add(new HorizontalProductScrollModel(documentSnapshot.get("product_ID_"+x).toString()
                                                , documentSnapshot.get("product_image_"+x).toString()
                                                ,documentSnapshot.get("product_title_"+x).toString()
                                                ,documentSnapshot.get("product_subtitle_"+x).toString()
                                                ,documentSnapshot.get("product_price_"+x).toString()));
                                    
                                    homePageModelList.add(new HomePageModel(3, documentSnapshot.get("layout_title").toString()
                                            , documentSnapshot.get("layout_background").toString(), gridLayoutModelList));

                                

                            
                            adapter.notifyDataSetChanged();
                        else
                            String error = task.getException().getMessage();
                            Toast.makeText(context, error,Toast.LENGTH_SHORT).show();
                        
                    
                );
    

这是主页适配器

package com.my.electroshop;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.gridlayout.widget.GridLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class HomePageAdapter extends RecyclerView.Adapter 

    private List<HomePageModel> homePageModelList;
    private RecyclerView.RecycledViewPool recycledViewPool;

   public   HomePageAdapter(List<HomePageModel> homePageModelList) 
        this.homePageModelList = homePageModelList;
        recycledViewPool = new RecyclerView.RecycledViewPool();
    

    @Override
    public int getItemViewType(int position) 
        switch (homePageModelList.get(position).getType()) 
            case 0:
                return HomePageModel.BANNER_SLIDER;
            case 1:
                return HomePageModel.STRIP_AD_BANNER;
            case 2:
                return HomePageModel.HORIZONTAL_PRODUCT_VIEW;
            case 3:
                return HomePageModel.GRID_PRODUCT_VIEW;
            default:
                return -1;
        
    

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        switch (viewType) 
            case HomePageModel.BANNER_SLIDER:
                View bannerSliderView = LayoutInflater.from(parent.getContext()).inflate(R.layout.sliding_ad_layout, parent, false);
                return new BannerSliderViewHolder(bannerSliderView);

            case HomePageModel.STRIP_AD_BANNER:
                View stripAdView = LayoutInflater.from(parent.getContext()).inflate(R.layout.strip_ad_layout, parent, false);
                return new StripAdBannerViewHolder(stripAdView);

            case HomePageModel.HORIZONTAL_PRODUCT_VIEW:
                View horizontalProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_scroll_layout, parent, false);
                return new HorizontalProductViewHolder(horizontalProductView);
            case HomePageModel.GRID_PRODUCT_VIEW:
                View gridProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_product_layout, parent, false);
                return new GridProductViewHolder(gridProductView);
            default:
                return null;
        
    

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 
        switch (homePageModelList.get(position).getType()) 
            case HomePageModel.BANNER_SLIDER:
                List<SliderModel> sliderModelList = homePageModelList.get(position).getSliderModelList();
                ((BannerSliderViewHolder) holder).setBannerSliderViewPager(sliderModelList);
                break;
            case HomePageModel.STRIP_AD_BANNER:
                String resource = homePageModelList.get(position).getResource();
                String color = homePageModelList.get(position).getBackgroundColor();
                ((StripAdBannerViewHolder) holder).setStripAd(resource, color);
                break;
            case HomePageModel.HORIZONTAL_PRODUCT_VIEW:
                String layoutColor = homePageModelList.get(position).getBackgroundColor();
                String horizontalLayoutTitle = homePageModelList.get(position).getTitle();
                List<WishListModel>viewAllProductList = homePageModelList.get(position).getViewAllProductList();
                List<HorizontalProductScrollModel> horizontalProductScrollModelList =homePageModelList.get(position).getHorizontalProductScrollModelList();
                ((HorizontalProductViewHolder) holder).setHorizontalProductLayout(horizontalProductScrollModelList, horizontalLayoutTitle ,layoutColor , viewAllProductList);
                break;
            case HomePageModel.GRID_PRODUCT_VIEW:
                String gridLayoutColor = homePageModelList.get(position).getBackgroundColor();
                String gridLayoutTitle = homePageModelList.get(position).getTitle();
                List<HorizontalProductScrollModel> gridProductScrollModelList =homePageModelList.get(position).getHorizontalProductScrollModelList();
                ((GridProductViewHolder) holder).setGridProductLayout(gridProductScrollModelList, gridLayoutTitle ,gridLayoutColor);
                break;
            default:
                return;
        
    

    @Override
    public int getItemCount() 
        return homePageModelList.size();
    

    public class BannerSliderViewHolder extends RecyclerView.ViewHolder 

        private ViewPager bannerSliderViewPager;
        private int currentPage;
        private Timer timer;
        final private long DELAY_TIME = 3000;
        final private long PERIOD_TIME = 3000;
        private List<SliderModel> arrangedList;

        public BannerSliderViewHolder(@NonNull View itemView) 
            super(itemView);
            bannerSliderViewPager = itemView.findViewById(R.id.banner_slider_view_pager);
        

        private void setBannerSliderViewPager(final List<SliderModel> sliderModelList) 
            currentPage = 2;
            if(timer!=null)
                timer.cancel();
            
            arrangedList = new ArrayList<>();
            for(int x = 0; x< sliderModelList.size() ; x++)
                arrangedList.add(x,sliderModelList.get(x));
            
            arrangedList.add(0,sliderModelList.get(sliderModelList.size() - 2));
            arrangedList.add(1,sliderModelList.get(sliderModelList.size() - 1));
            arrangedList.add(sliderModelList.get(0));
            arrangedList.add(sliderModelList.get(1));


            SliderAdapter sliderAdapter = new SliderAdapter(arrangedList);
            bannerSliderViewPager.setAdapter(sliderAdapter);
            bannerSliderViewPager.setClipToPadding(false);
            bannerSliderViewPager.setPageMargin(20);
            bannerSliderViewPager.setPageMargin(currentPage);


            ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() 
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 


                

                @Override
                public void onPageSelected(int position) 
                    currentPage = position;
                

                @Override
                public void onPageScrollStateChanged(int state) 
                    if (state == ViewPager.SCROLL_STATE_IDLE) 
                        pageLooper(arrangedList);
                    

                
            ;
            bannerSliderViewPager.addOnPageChangeListener(onPageChangeListener);
            startBannerSlideShow(arrangedList);

            bannerSliderViewPager.setOnTouchListener(new View.OnTouchListener() 
                @Override
                public boolean onTouch(View v, MotionEvent event) 
                    pageLooper(arrangedList);
                    stopBannerSlideShow();
                    if (event.getAction() == MotionEvent.ACTION_UP) 
                        startBannerSlideShow(arrangedList);
                    
                    return false;
                
            );
        

        private void pageLooper(List<SliderModel> sliderModelList) 
            if (currentPage == sliderModelList.size() - 2) 
                currentPage = 2;
                bannerSliderViewPager.setCurrentItem(currentPage, false);
            
            if (currentPage == 1) 
                currentPage = sliderModelList.size() - 3;
                bannerSliderViewPager.setCurrentItem(currentPage, false);
            
        

        private void startBannerSlideShow(final List<SliderModel> sliderModelList) 
            final Handler handler = new Handler();
            final Runnable update = new Runnable() 
                @Override
                public void run() 
                    if (currentPage >= sliderModelList.size()) 
                        currentPage = 1;
                    
                    bannerSliderViewPager.setCurrentItem(currentPage++, true);

                
            ;
            timer = new Timer();
            timer.schedule(new TimerTask() 
                @Override
                public void run() 
                    handler.post(update);
                
            , DELAY_TIME, PERIOD_TIME);
        

        private void stopBannerSlideShow() 
            timer.cancel();
        
    

    public class StripAdBannerViewHolder extends RecyclerView.ViewHolder 
        private ImageView stripAdImage;
        private ConstraintLayout stripAdContainer;

        public StripAdBannerViewHolder(@NonNull View itemView) 
            super(itemView);
            stripAdImage = itemView.findViewById(R.id.strip_ad_image);
            stripAdContainer = itemView.findViewById(R.id.strip_ad_container);
        

        private void setStripAd(String  resource, String color) 
            Glide.with(itemView.getContext()).load(resource).apply(new RequestOptions().placeholder(R.mipmap.search)).into(stripAdImage);
            stripAdContainer.setBackgroundColor(Color.parseColor(color));
        
    

    public class HorizontalProductViewHolder extends RecyclerView.ViewHolder 

        private ConstraintLayout container;
        private TextView horizontalLayoutTitle;
        private Button horizontalViewAllBtn;
        private RecyclerView horizontalRecyclerView;

        public HorizontalProductViewHolder(@NonNull View itemView) 
            super(itemView);
            container = itemView.findViewById(R.id.container);
            horizontalLayoutTitle = itemView.findViewById(R.id.horizontal_scroll_layout_title);
            horizontalViewAllBtn = itemView.findViewById(R.id.horizontal_scroll_view_all_btn);
            horizontalRecyclerView = itemView.findViewById(R.id.horizontal_scroll_layout_recyclerview);
            horizontalRecyclerView.setRecycledViewPool(recycledViewPool);
        

        private void setHorizontalProductLayout(List<HorizontalProductScrollModel> horizontalProductScrollModelList, final String title , String color , final List<WishListModel> viewAllProductList) 
            container.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
            horizontalLayoutTitle.setText(title);
            if(horizontalProductScrollModelList.size()>8)
                horizontalViewAllBtn.setVisibility(View.VISIBLE);
                horizontalViewAllBtn.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        ViewAllActivity.wishListModelList = viewAllProductList;
                        Intent viewAllIntent = new Intent(itemView.getContext(),ViewAllActivity.class);
                        viewAllIntent.putExtra("Layout_code",0);
                        viewAllIntent.putExtra("title",title);
                        itemView.getContext().startActivity(viewAllIntent);
                    
                );
            else 
                horizontalViewAllBtn.setVisibility(View.INVISIBLE);
            
            HorizontalProductScrollAdapter horizontalProductScrollAdapter = new HorizontalProductScrollAdapter(horizontalProductScrollModelList);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(itemView.getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            horizontalRecyclerView.setLayoutManager(linearLayoutManager);
            horizontalRecyclerView.setAdapter(horizontalProductScrollAdapter);
            horizontalProductScrollAdapter.notifyDataSetChanged();
        
    

    public class GridProductViewHolder extends RecyclerView.ViewHolder
        private ConstraintLayout container;
        private TextView gridLayoutTitle;
        private Button gridLayoutViewAllBtn;
        private GridLayout gridProductLayout;

        public GridProductViewHolder(@NonNull View itemView) 
            super(itemView);
             container =itemView.findViewById(R.id.container);
             gridLayoutTitle = itemView.findViewById(R.id.grid_product_layout_title);
             gridLayoutViewAllBtn = itemView.findViewById(R.id.grid_product_layout_viewall_btn);
             gridProductLayout = itemView.findViewById(R.id.grid_layout);

        
        private void setGridProductLayout(final List<HorizontalProductScrollModel> horizontalProductScrollModelList, final String title , String color)
            container.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
            gridLayoutTitle.setText(title);

            for(int x = 0; x < 4 ; x++)
                ImageView productImage = gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_image);
                TextView productTitle = gridProductLayout.getChildAt(x).findViewById(R.id.h_s_prodrct_title);
                TextView productDescription = gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_discription);
                TextView productPrice = gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_price);

                Glide.with(itemView.getContext()).load(horizontalProductScrollModelList.get(x).getProductImage()).apply(new RequestOptions().placeholder(R.mipmap.search)).into(productImage);
                productTitle.setText(horizontalProductScrollModelList.get(x).getProductTitle());
                productDescription.setText(horizontalProductScrollModelList.get(x).getProductDescription());
                productPrice.setText("Rs. "+horizontalProductScrollModelList.get(x).getProductPrice()+"/-");
                gridProductLayout.getChildAt(x).setBackgroundColor(Color.parseColor("#ffffff"));

                gridProductLayout.getChildAt(x).setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        Intent productDetailsIntent = new Intent(itemView.getContext(),ProductDetailsActivity.class);
                        itemView.getContext().startActivity(productDetailsIntent);
                    
                );
            

            gridLayoutViewAllBtn.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    ViewAllActivity.horizontalProductScrollModelList = horizontalProductScrollModelList;
                    Intent viewAllIntent = new Intent(itemView.getContext(),ViewAllActivity.class);
                    viewAllIntent.putExtra("Layout_code",1);
                    viewAllIntent.putExtra("title", title);
                    itemView.getContext().startActivity(viewAllIntent);
                
            );
        

    

这是查看所有活动:

package com.my.electroshop;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ViewAllActivity extends AppCompatActivity 

    private RecyclerView recyclerView;
    private GridView gridView;
    public static List<HorizontalProductScrollModel> horizontalProductScrollModelList;
    public static List<WishListModel> wishListModelList;



    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setTitle(getIntent().getStringExtra("title"));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        recyclerView = findViewById(R.id.recycler_view);
        gridView = findViewById(R.id.grid_view);

        int layout_code = getIntent().getIntExtra("layout_code",-1);

        if(layout_code == 0) 
            recyclerView.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);
            WishListAdapter adapter = new WishListAdapter(wishListModelList, false);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
         else if (layout_code == 1) 
           gridView.setVisibility(View.VISIBLE);
            GridProductLayoutAdapter gridProductLayoutAdapter = new GridProductLayoutAdapter(horizontalProductScrollModelList);
            gridView.setAdapter(gridProductLayoutAdapter);
        
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        if(item.getItemId() == android.R.id.home)
            finish();
            return true;
        
        return super.onOptionsItemSelected(item);
    


在错误部分,当我切换以查看所有活动时将显示此>

我从Firebase查询数据,并将其添加到horizo​​ntalscrollmodellist和viewalllist中,并传递到主页适配器以在网格布局和水平滚动布局中显示,它完美地显示了查询的数据...

答案

我也有同样的问题,如果您有解决方案,请帮助我

以上是关于从火力基地查询的数据不会显示在recyclerview和grid视图中的主要内容,如果未能解决你的问题,请参考以下文章

我是火力基地的新手。我无法从 firebase 检索数据

从火力基地检索信息

火力基地 |远程通知不显示,swift 3.0

如何根据用户的帐户注册条件显示组件?火力基地 | Vue

如何获得具有特定属性的特定孩子..这是我的火力基地

从其他特定集合中获取文档计数颤振火力基地