1

I was making an app where it fetches specific data from the database to the app and display it in a recycler view, but I have a problem that I don't know how to fix, I'm pretty sure it has to do with the HomeFragment.java. Please check the multi-line comment near the end of the script. I will add the GitHub Repo because I don't think it is a single-file error.

Sorry for the pain and suffering you might encounter in advance for being so vague, but I don't know how to say it any better.

BlogRecyclerAdapter.Java: https://pastebin.com/39G2mEW5

HomeFragment.Java: https://pastebin.com/YmKs5QUJ

Thank You!

REPO: https://github.com/hjdaboss123/BlindNews

Path to HomeFragment.javaBlind:News/app/src/main/java/com/blindnews/kimh2/blindnews

EDIT1: Added pastebin for code on Recycler Adapter and HomeFragment

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Hyungjun
  • 65
  • 7

1 Answers1

2

This isn't the full solution to the problem but in comments we figured out the problem which was, OP using Firestore to get data from Realtime database.

Change this in fragment -

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    blog_list = new ArrayList<>();
    blog_list_view = view.findViewById(R.id.blog_list_view);

    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blog_list_view.setLayoutManager(new LinearLayoutManager(container.getContext()));
    blog_list_view.setAdapter(blogRecyclerAdapter);

    firebaseFirestore = FirebaseFirestore.getInstance();
    firebaseFirestore.collection("articles").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                if (doc.getType() == DocumentChange.Type.ADDED) {
                    BlogPost blogPost = doc.getDocument().toObject(BlogPost.class);
                    blog_list.add(blogPost);
                }
            }
            Log.d("HomeFragment", "onCreateView: " + blog_list.size());
            //Send updated list to adapter.
            blogRecyclerAdapter.updatePosts(blog_list);
        }
    });

    return view;
}

Handle updated list inside adapter -

public void updatePosts(List<BlogPost> blogPostList) {
    //if you want to update the whole list. If you want to append, List has addAll method I think and use it with notifyItemRangeInserted for better performance.
    this.blog_list = blogPostList;
    notifyDataSetChanged();
}
Ranjan
  • 1,096
  • 10
  • 22
  • Hmm, the code still doesn't show what I want it to show, could it be a Firebase permissions issue? or is it still purely my code. – Hyungjun Feb 16 '19 at 11:08
  • 1
    Add a log just before updating the adapter and check the size of the list. This log will appear in logcat, Use tag to search if you don't find it. Added in the solution. – Ranjan Feb 16 '19 at 11:09
  • it comes up to 2019-02-16 19:10:46.443 22979-22979/com.blindnews.kimh2.blindnews D/HomeFragment: onCreateView: 0 2019-02-16 19:10:46.443 22979-22979/com.blindnews.kimh2.blindnews D/HomeFragment: onCreateView: 0 Is there a way to retrieve the list of posts in the database regardless of whether one has been added or not? – Hyungjun Feb 16 '19 at 11:12
  • 1
    What if you remove the if condition? `if (doc.getType() == DocumentChange.Type.ADDED)`. – Ranjan Feb 16 '19 at 11:16
  • Wait, it still says onCreateView: 0. Is there a different for loop i could use? Like finding just elements instead of documentChanges? – Hyungjun Feb 16 '19 at 11:18
  • 1
    Seems like you are actually not getting documents from `Firestore`. Do you have documents in `Firestore`? – Ranjan Feb 16 '19 at 11:24
  • 1
    Can you check from firebase console whether you have documents in `articles` collection in Firestore? – Ranjan Feb 16 '19 at 11:26
  • 1
    But this looks like `Realtime Database` and you are using `Firestore` in your code, They are different. – Ranjan Feb 16 '19 at 11:32
  • ahhhhhh. hm, is there a simple way to chage from firestore to realtime database? or viceverca? – Hyungjun Feb 16 '19 at 11:34
  • 1
    Nope, If you are just starting with app side, shifting to `Realtime database` is easy and you don't have to change(backend logic) how you are storing in db. https://firebase.google.com/docs/database/android/start – Ranjan Feb 16 '19 at 11:36
  • would it be as simple as export, import? or do i need to do more – Hyungjun Feb 16 '19 at 11:38
  • 1
    On the mobile side, it will be like using a different library(adding 1 line in dependency) and 4-5 lines of code in `HomeFragment`. – Ranjan Feb 16 '19 at 11:39
  • 1
    Although Firebase is moving onto Firestore, so you can also use it if you are in early stage of development, because it will have better support in future. Also there could be a solution to move your `realtime database` data to `Firestore`. But read how different they are, they have different pricing plans https://firebase.google.com/docs/database/rtdb-vs-firestore. – Ranjan Feb 16 '19 at 11:46
  • also, in case you DO know, i was wondering if you know how to solve this part of the code `BlogPost blogPost = doc.getDocument().toObject(BlogPost.class);` – Hyungjun Feb 16 '19 at 11:53
  • I can't seem to anything that can get the data from a DataSnapshot – Hyungjun Feb 16 '19 at 12:06
  • 1
    I follow this document to get all documents from a collection in `Firestore`. https://firebase.google.com/docs/firestore/query-data/get-data. Yeah I also use `toObject()` method to cast result to a list. – Ranjan Feb 16 '19 at 12:12
  • but I need it to be a realtime database, is there a alternative that is for the RTDB? – Hyungjun Feb 16 '19 at 12:13
  • 1
    Refer this document https://firebase.google.com/docs/database/android/lists-of-data. This might be more helpful https://stackoverflow.com/questions/38652007/how-to-retrieve-specific-list-of-data-from-firebase. – Ranjan Feb 16 '19 at 12:15