首页 > 解决方案 > 如何为一个项目使用一张卡片,而不是让所有卡片都填充数据库中的最后一项?

问题描述

我有这个片段:

    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": "Big Ben", "image_link": "gs://for-tourists-by-tourists.appspot.com/images/big_ben.png", "full_description": "虽然有夏季休会期间的议会大厦(又名威斯敏斯特宫)可供所有人使用,英国居民可以参观......" } }

在我的应用程序中,我所有的卡片都会更新标题为“大本钟”和该图像,请帮助我在每张卡片中添加不同的项目。
奇怪的是我只有 4 张卡(我应该)

标签: androidfirebasefirebase-realtime-databaseandroid-recyclerview

解决方案


推荐阅读