0

How To parse Firebase JSON to show on RecyclerView?

enter image description here

What is wrong in below code:

 databaseJobPost = FirebaseDatabase.getInstance().getReference("job_post");
 databaseJobPost.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            addPostListModels.clear();
            for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                System.out.println("university"+postSnapshot.getKey());
                AddPostModel university = postSnapshot.getValue(AddPostModel.class);
                addPostListModels.add(university);

                // here you can access to name property like university.name
            }

            jobLisitingAdapter=new JobLisitingAdapter(JobListingAcitivity.this,addPostListModels);
            rvPostRecycler.setAdapter(jobLisitingAdapter);



        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
android tech
  • 13
  • 1
  • 3
  • Welcome to Stack Overflow! Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Grimthorr Nov 15 '18 at 09:33
  • If you see something in console by println? – Vadim Eksler Nov 15 '18 at 09:35
  • How have you defined `databaseJobPost`? – PradyumanDixit Nov 15 '18 at 09:37
  • @PradyumanDixit databaseJobPost = FirebaseDatabase.getInstance().getReference("job_post"); – android tech Nov 15 '18 at 09:43
  • Does `System.out.println("university"+postSnapshot.getKey());` return something? What are your expectations regarding your code? Please also responde with @AlexMamo – Alex Mamo Nov 15 '18 at 10:01
  • @AlexMamo I am getting data in System.out.println("university"+postSnapshot.getKey()); but i want to parse whole json and show it on recyclerview – android tech Nov 15 '18 at 10:08
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Nov 15 '18 at 10:12
  • @androidtech I assume your `AddPostModel` is trying to match the JSON. If it doesn't work, it'll be easier to help if you show the code for that class. In general, please read [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve), since it drastically improves the changes that you get help quickly. – Frank van Puffelen Nov 15 '18 at 14:19
  • Actualy the datasnapshot that comes back is not a JSON... I don't know why the firebase plugin doesn't just return a JSON... It would be too simple! Add a println of your datasnapshot and you will see that it always starts with "Datasnapshot [...." – Christophe Chenel Nov 15 '18 at 15:56

0 Answers0