使用查询填充firebaseRecyclerAdapter

时间:2019-02-14 19:38:12

标签: java android firebase firebase-realtime-database

我目前正在开发一个应用,用户可以在其中收藏自己喜欢的项目,以后可以在其他活动中查看它们。开发此程序时,我陷入了应用程序显示收藏项的最后一部分。 考虑以下数据结构:

JSON Database structure

下面提供的代码显示了我如何使用FirebaseRecyclerAdapter。首先,查询按预期工作,它返回一个对象。我的问题是如何以一种不仅显示最喜欢的项目,而且显示所有项目的方式来实现这一目标。 (这些项目位于不同的类别中)例如,类似这样的事情将发送一个新查询并将新项目添加到适配器:

for(int i=0; i<itemList.size(); i++) {
    Query listRef = FirebaseDatabase.getInstance().getReference("Items").child("Category").child(itemList.get(i)).orderByChild("id").equalTo(itemList.get(i++)).limitToFirst(10);
   //rest of the code
}
   private void showData(){
/*
itemList is an array that has on:
index 0 category
index 1 item_id
index 2 category
index 3 item_id
and so on from Users/Likes
*/
            Query listRef = FirebaseDatabase.getInstance().getReference("Items").child("Category").child(itemList.get(0)).orderByChild("id").equalTo(itemList.get(1)).limitToFirst(10);
            options = new FirebaseRecyclerOptions.Builder<Model>().setQuery(listRef,Model.class).build();
            firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) {
                    holder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getImage(), model.getRating(), model.getViews(), model.getId(), model.getLikes());
                }

                @NonNull
                @Override
                public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
                    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
                    ViewHolder viewHolder = new ViewHolder(itemView);
                    viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            //get Data from firebase at the position clicked
                            String mTitle = getItem(position).getTitle();
                            String mDesc = getItem(position).getDescription();
                            String mImage = getItem(position).getImage();
                            String mRating = getItem(position).getRating();
                            String mViews = getItem(position).getViews();
                            String mItem_id = getItem(position).getId();
                            String mLikes = getItem(position).getLikes();
                        }

                        @Override
                        public void onItemLongClick(View view, int position) {

                        }
                    });
                    return viewHolder;
                }
            };
            mRecyclerView.setLayoutManager(mLayoutManager);
            firebaseRecyclerAdapter.startListening();
            //set adapter to firebase recycler view
            mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    }

0 个答案:

没有答案