Firebase Firestore仅检索一项而不是多项

时间:2019-02-01 07:35:44

标签: java android firebase android-studio google-cloud-firestore

我正在开发一款移动电话,该移动电话使用Sticky Headers RecyclerView列出药品,并且我正在从Firebase firetore云数据库中获取这些物品(药品清单),但是它只带一个物品,而不是指定集合中的所有物品< / p>

这是我必须显示药物的片段中的密码

public class DawaFragment extends Fragment {

    private RecyclerView dawaList;
    private ArrayList<Dawa> dawaPostList;
    FirebaseFirestore firebaseFirestore;
    private DawaRecyclerAdapter adapter;
    LinearLayoutManager layoutManager;
    private DocumentSnapshot lastVisible;

    private Boolean isFirstPageFirstLoad = true;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK)
        {
            String group_character_clicked = data.getStringExtra("result");
            int position = Common.findPositionWithName(group_character_clicked,dawaPostList);

            dawaList.smoothScrollToPosition(position);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_dawa, container, false);

        dawaPostList = new ArrayList<>();
        dawaList = view.findViewById(R.id.dawaList);


        layoutManager = new LinearLayoutManagerWithSmoothScroller(getContext());
        dawaList.setLayoutManager(layoutManager);
        dawaList.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getContext()),layoutManager.getOrientation()));
        createMedicine();
        adapter = new DawaRecyclerAdapter(getContext(), dawaPostList);
        dawaList.setAdapter(adapter);





        return view;
    }

    private void createMedicine() {
        if(FirebaseAuth.getInstance().getCurrentUser() != null) {

            firebaseFirestore = FirebaseFirestore.getInstance();

            dawaList.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    Boolean reachBottom = !recyclerView.canScrollVertically(1);

                    if (reachBottom)
                    {
                       loadMorePost();

                    }
                }
            });

            Task<QuerySnapshot> firstQuery = firebaseFirestore.collection("Dawa").limit(30).get();

            firstQuery.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot documentSnapshots) {
                    if (documentSnapshots != null) {
                        if (!documentSnapshots.isEmpty()) {

                            if (isFirstPageFirstLoad) {

                                lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                                dawaPostList.clear();

                            }

                            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                                if (doc.getType() == DocumentChange.Type.ADDED) {

                                    String blogPostId = doc.getDocument().getId();
                                    Dawa blogPost = doc.getDocument().toObject(Dawa.class).withId(blogPostId);

                                    if (isFirstPageFirstLoad) {

                                        dawaPostList.add(blogPost);

                                        dawaPostList = Common.sortList(dawaPostList);
                                        dawaPostList = Common.addAlphabets(dawaPostList);

                                    } else {

                                        dawaPostList.add(0, blogPost);
                                        dawaPostList = Common.sortList(dawaPostList);
                                        dawaPostList = Common.addAlphabets(dawaPostList);

                                    }


                                    adapter.notifyDataSetChanged();

                                }
                            }

                            isFirstPageFirstLoad = false;

                        }
                    }
                }
            });

        }

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void loadMorePost() {

        Task<QuerySnapshot> nextQuery = firebaseFirestore.collection("Dawa").startAfter(lastVisible)
                .limit(30).get();

        nextQuery.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {
                if(documentSnapshots != null) {

                    if (!documentSnapshots.isEmpty()) {

                        lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                        for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                            if (doc.getType() == DocumentChange.Type.ADDED) {

                                String blogPostId = doc.getDocument().getId();
                                Dawa blogPost = doc.getDocument().toObject(Dawa.class).withId(blogPostId);
                                dawaPostList.add(blogPost);

                                dawaPostList = Common.sortList(dawaPostList);
                                dawaPostList = Common.addAlphabets(dawaPostList);

                                adapter.notifyDataSetChanged();
                            }

                        }
                    }
                }
            }
        });


    }

}

以下是我的适配器的代码

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

    private List<Dawa> dawa_list;
    public Context context;

    public DawaRecyclerAdapter(Context context, List<Dawa> dawa_list) {

            this.context = context;
            this.dawa_list = dawa_list;

    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);

        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dawa_item_layout, parent, false);

        if(viewType == VIEWTYPE_GROUP)
        {
            ViewGroup group = (ViewGroup)inflater.inflate(R.layout.group_layout,viewGroup,false);
            return new GroupViewHolder(group);
        }
        else if(viewType == VIEWTYPE_PERSON)
        {
            ViewGroup group = (ViewGroup)inflater.inflate(R.layout.dawa_layout,viewGroup,false);
            return new PersonViewHolder(group);
        }
        else
        {
            ViewGroup group = (ViewGroup)inflater.inflate(R.layout.group_layout,viewGroup,false);
            return new GroupViewHolder(group);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dawa_list.get(position).getViewType();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {

        if (viewHolder instanceof GroupViewHolder)
        {
            GroupViewHolder groupViewHolder = (GroupViewHolder) viewHolder;
            groupViewHolder.textGroup.setText(dawa_list.get(position).getName());
            groupViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //start action to display al alphabets

                    ((Activity)context).startActivityForResult(new Intent(context, AlphabetActivity.class), RESULT_CODE);
                }
            });
        }
        else  if (viewHolder instanceof PersonViewHolder)
        {
            final PersonViewHolder personViewHolder = (PersonViewHolder) viewHolder;
            personViewHolder.dawaName.setText(dawa_list.get(position).getName());

            //Get Avatar
            ColorGenerator generator = ColorGenerator.MATERIAL;
            TextDrawable drawable = TextDrawable.builder().buildRound(String.valueOf(dawa_list.get(position).getName()
                    .charAt(0)),generator.getRandomColor());

            personViewHolder.dawaAvatar.setImageDrawable(drawable);

            personViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent commentIntent = new Intent(context, DawaDescsActivity.class);
                    Bundle extras = new Bundle();
                    extras.putString("article", dawa_list.get(position).getDescs());
                    extras.putString("title", dawa_list.get(position).getName());

                    commentIntent.putExtras(extras);

                    context.startActivity(commentIntent);                }
            });

        }

    }

    @Override
    public int getItemCount() {
        return dawa_list.size();
    }

    private class GroupViewHolder extends RecyclerView.ViewHolder{

        private TextView textGroup;

        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);

            textGroup = itemView.findViewById(R.id.textGroup);

        }
    }

    private class PersonViewHolder extends RecyclerView.ViewHolder{

        private TextView dawaName;
        private ImageView dawaAvatar;

        public PersonViewHolder(View itemView) {
            super(itemView);

            dawaName = itemView.findViewById(R.id.dawa_name);
            dawaAvatar = itemView.findViewById(R.id.dawa_avatar);

        }
    }
}

0 个答案:

没有答案