如何为一张物品购买一张卡片,而不是用数据库中的最后一个物品填充所有卡片?

时间:2019-03-09 18:46:59

标签: android firebase firebase-realtime-database android-recyclerview

我有这个片段:

    public class AttractionsFragment extends Fragment {

        Context context;
        private View LocationView;
        private RecyclerView myLocationsRecycler;
        private DatabaseReference ModelRef;

        public AttractionsFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


        }

        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            LocationView = inflater.inflate(R.layout.fragment_blank, container, false);


            myLocationsRecycler = LocationView.findViewById(R.id.cat_recycler);
            myLocationsRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
            FirebaseApp.initializeApp(context);
            ModelRef = FirebaseDatabase.getInstance().getReference().child("attraction_list");


            return LocationView;
        }

        @Override
        public void onStart() {
            super.onStart();

            FirebaseRecyclerOptions<Model> options = new FirebaseRecyclerOptions.Builder<Model>()
                    .setQuery(ModelRef, Model.class)
                    .build();

            FirebaseRecyclerAdapter<Model, ModelViewHolder> adapter = new FirebaseRecyclerAdapter<Model, ModelViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final ModelViewHolder holder,  int position, @NonNull Model model) {


                    ModelRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for(DataSnapshot data: dataSnapshot.getChildren()){
                                String name = data.child("name").getValue().toString();
                                String image=data.child("image_link").getValue().toString();

                                holder.title.setText(name);
                                Picasso.get().load(image).into(holder.imageBg);


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

                    }
                });

            }

            @NonNull
            @Override
            public ModelViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_item_layout, viewGroup, false);
                return new ModelViewHolder(view);
            }
        };
        myLocationsRecycler.setAdapter(adapter);
        adapter.startListening();
    }


    public static class ModelViewHolder extends RecyclerView.ViewHolder {

        ImageView imageBg;
        TextView title;

        public ModelViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            imageBg = itemView.findViewById(R.id.image);
        }

    }

}

这个数据库

{
  "attraction_list": {
    "01":{
       "name": "Eye of London",
       "image_link": "gs://for-tourists-by-tourists.appspot.com/images/eye_london.png",
       "full_description": "As iconic for London as the Eiffel Tower is to Paris, the Coca-Cola London Eye is a spectacular testament to modern Britain. At 135 metres, the London Eye is one of the world's tallest observation wheels, providing up to 40 kilometres of panoramic views on a clear day. The gradual rotation in one of the 32 high-tech glass capsules takes approximately 30 minutes, offering breathtaking views of London and its famous landmarks. Whether it’s with family, friends or maybe a romantic Champagne Experience just for two, London Eye tickets are an integral part of any trip to London. See more of London for less with 365Tickets exclusive London Eye combination tickets."
    },
"02":{
   "name": "Tower Bridge",
   "image_link": "gs://for-tourists-by-tourists.appspot.com/images/tower_bridge.png",
   "full_description": "Completed in 1894 The Most Famous Bridge in the World is a marvel of Victorian engineering and the home of the Tower Bridge Exhibition. At 42 metres above the river Thames, a visit to the Tower Bridge exhibition boasts spectacular panoramic views of some of London’s most famous landmarks: The London Eye, Canary Wharf, and St Paul’s Cathedral can all be seen from its high-level walkways – and the budding photographers will love that the specially designed windows make capturing that iconic image an easy task. Visitors to the exhibition will discover the rich history of Tower Bridge with an ever changing variety of content throughout the year."
},
"03":{
   "name": "Buckingham Palace",
   "image_link": "gs://for-tourists-by-tourists.appspot.com/images/buckingham.png",
   "full_description": "Since 1837, Buckingham Palace has served as the official London residence of Britain's kings and queens. Today, we know it as the administrative headquarters of Her Majesty, Queen Elizabeth II."
},
"04":{
   "

name”:“大笨钟”,    “ image_link”:“ gs://for-tourists-by-tourists.appspot.com/images/big_ben.png”,    “ full_description”:“尽管在夏季休假期间向国会大厦(又名威斯敏斯特宫)进行游览是收费的,但所有人都可以使用,但是英国居民可以进行游览...” } }

在我的应用中,我所有的卡片都获得了标题为“大笨钟”和该图片的更新,请帮助我为每张卡片添加一个不同的物品。
奇怪的是,我只有四张牌(我应该如此)

0 个答案:

没有答案