Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

android - how to retrieve data to a recycler view from 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());
        }
    });
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...