如何从 FIREBASE 检索数据到回收站视图 [英] how to retrieve data to a recycler view from FIREBASE

查看:11
本文介绍了如何从 FIREBASE 检索数据到回收站视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_home);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.menu_recycler);
    recyclerView.setHasFixedSize(true);

    LinearLayoutManager linearLayout = new LinearLayoutManager(this);
    linearLayout.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayout);

    MenuHomeAdapter menuHomeAdapter = new MenuHomeAdapter(this, createList(7));

    recyclerView.setAdapter(menuHomeAdapter);

    //FireBase
    Firebase.setAndroidContext(this);
    // Get a reference to our posts
    Firebase ref = new Firebase("LINK");

    // Attach an listener to read the data at our posts reference
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Menu post = postSnapshot.getValue(Menu.class);
                Toast.makeText(MenuHome.this, post.getSNo() + "-" + post.getDishName(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });
}

如何在回收者视图中显示数据.我尝试了一些方法,但不正确.Toast 可以工作,但所有其他选项都是错误的.我只想在回收者视图中显示来自 firebase 的数据.

how to display the data in the recycler view.I tried some methods but it was not proper.the Toast works but all other option are wrong.I just want to display the data from firebase in a recycler view.

推荐答案

正如 helldawg 所说,您需要创建自己的适配器:

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

  • 监听来自 Firebase 的 onDataChange 调用
  • 保留 Firebase 数据的内部副本
  • 当数据发生变化时通知RecyclerView
  • 在需要时创建视图持有者
  • 将 Firebase 数据中的数据绑定到视图

我能快速想到的最低限度是:

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();
    }
};

要在上下文中查看它,请查看 Activity34962254(以您的问题编号命名)在 这个 Github 存储库.

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.

除非您有特定需求,否则您最好使用 使用来自 FirebaseUI 项目的 FirebaseRecyclerAdapter.为了全面了解所提供的内容,我推荐这个 codelab 用于构建聊天应用程序Firebase 界面

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

这篇关于如何从 FIREBASE 检索数据到回收站视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆