how to retrieve data to a recycler view from FIREBASE

后端 未结 3 385
走了就别回头了
走了就别回头了 2020-12-16 08:51
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_home);


    Toolbar         


        
相关标签:
3条回答
  • 2020-12-16 09:21

    As helldawg commented, you'll need to create your own adapter that:

    • listens to onDataChange calls from Firebase
    • keeps an internal copy of the data from Firebase
    • informs the RecyclerView when the data changes
    • creates view holders when those are needed
    • binds data from the Firebase data to the view

    The minimum I could come up with quickly is:

    public static class MenuHomeAdapter extends RecyclerView.Adapter<MenuViewHolder> {
        ArrayList<Menu> items = new ArrayList<>();
    
        public MenuHomeAdapter(Firebase ref) {
            ref.addValueEventListener(new ValueEventListener() {
                public void onDataChange(DataSnapshot snapshot) {
                    items.clear();
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        Menu menu = postSnapshot.getValue(Menu.class);
                        items.add(menu);
                    }
                    notifyDataSetChanged();
                }
    
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });
        }
    
        public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
            return new MenuViewHolder(view);
        }
    
        public void onBindViewHolder(MenuViewHolder holder, int position) {
            Menu item = items.get(position);
            holder.getTitleView().setText(item.getTitle());
        }
    
        public int getItemCount() {
            return items.size();
        }
    };
    

    To see it in context, look at Activity34962254 (named after the number of your question) on this Github repo.

    You'll note that implementing this is fairly non-trivial. And while this implementation works, it is pretty sub-optimal. For example: when a single item is changed/added/deleted, the entire list is rebuilt. That's a recipe for bad performance.

    Unless you have specific needs, you're probably better off using the FirebaseRecyclerAdapter from the FirebaseUI project. To get a full walkthrough of what that offers, I recommend this codelab for building a chat app with FirebaseUI

    0 讨论(0)
  • 2020-12-16 09:21

    There is a pretty good tutorial here

    https://github.com/firebase/FirebaseUI-Android#using-firebaseui-to-populate-a-recyclerview

    That will give you what you need, and also FirebaseUI gives you a lot to play with.

    Now if you want to include databinding as well:

    Custom ViewHolder

    public class YourViewHolder extends RecyclerView.ViewHolder {
    
        public YourItemBinding binding;
    
        public YourViewHolder(View itemView) {
            super(itemView);
    
            binding = DataBindingUtil.bind(itemView);
        }
    
        public YourItemBinding getBinding() {
            return binding;
        }
    }
    

    FirebaseRecyclerAdapter

    mAdapter = new FirebaseRecyclerAdapter <YourFirebaseItem, YourViewHolder> (YourFirebaseItem.class, android.R.layout.two_line_list_item, YourViewHolder.class, mRef) {
    
        @Override
        public void populateViewHolder(YourViewHolder viewHolder, YourFirebaseItem item, int position) {
    
            YourItemBinding binding = viewHolder.getBinding();
    
            binding.setItem(item);
    
        }
    };
    
    recycler.setAdapter(mAdapter);
    
    0 讨论(0)
  • 2020-12-16 09:28

    Why not simply using the FirebaseRecyclerAdapter ?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    RecyclerView messages = (RecyclerView) findViewById(R.id.messages);
    messages.setLayoutManager(new LinearLayoutManager(this));
    
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    
    mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
            Chat.class,
            R.layout.message,
            ChatHolder.class,
            ref) {
        @Override
        public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
            holder.setName(chat.getName());
            holder.setMessage(chat.getMessage());
        }
    };
    
    messages.setAdapter(mAdapter);
    }
    
    0 讨论(0)
提交回复
热议问题