4ice
4ice

Reputation: 171

Read data from firebase, and populate in listview

I am developing an app in which I want to read data from my firebase database, and put it in my listView.

there are 3 different read-actions I want to have.

  1. When the app is first started, I want to get the 10 most recent rows read.

  2. When I reach the end of the listView, I want to get 10 more rows, starting at the oldest one that has been read so far.

  3. when I pull to the top of the listView, I want to get all the rows that has been made after the row that is now displayed as the first one. (Is that understandable...?)

Note: I have already implemented listeners for when I read the top and the end of the listview, so it's only the firebase-calls that I need.

How should this be done?

Upvotes: 3

Views: 7455

Answers (1)

4ice
4ice

Reputation: 171

I have solved the issue, and here is what I have done.

1. When the app is first started, I want to get the 10 most recent rows read.

//read the 15 latest posts from the db
postQuery.limit(15).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //Read each child of the user
        for(DataSnapshot child : dataSnapshot.getChildren())
        {
            //If this is the first row we read, then this will be the oldest post that is going to be read,
            //Therefore we save the name of this post for further readings.
            if(prevOldestPostId.equals(oldestPostId))
            {
                //Save the name of the last read post, for later readings
                oldestPostId = child.getName();
            }
            //This if-statment is just a fail-safe, since I have an counter in the post, and that one I do not want to read.
            if(child.getName() != "nrOfPosts")
            {
                //Gather the data from the database
                UserPost readPost = new UserPost();
                for(DataSnapshot childLvl2 : child.getChildren()) {
                    if(childLvl2.getName() == "post")
                        readPost.setMessage(childLvl2.getValue().toString());
                    else
                        readPost.setDate(childLvl2.getValue().toString());
                }
                //Add the data to a temporary List
                toAdd.add(0, readPost);
            }
            //Keep the name of the newest post that has been read, we save this for further readings.
            newestPostId = child.getName();
        }
        //prevOlddestPostId is helping us to keep the the currently oldest post-name.
        prevOldestPostId = oldestPostId;
        //Add the new posts from the database to the List that is connected to an adapter and later on a ListView.
        for (UserPost aToAdd : toAdd) thePosts.add(aToAdd);

        // We need notify the adapter that the data have been changed
        mAdapter.notifyDataSetChanged();

        // Call onLoadMoreComplete when the LoadMore task, has finished
        mListView.onRefreshComplete();
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

2. When I reach the end of the listView, I want to get 10 more rows, starting at the oldest one that has been read so far.

postQuery.endAt(1, oldestPostId).limit(15).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //Gather the data
        for(DataSnapshot child : dataSnapshot.getChildren())
        {
            if(prevOldestPostId.equals(oldestPostId) && !child.getName().equals("nrOfPosts"))
            {
                //Save the name of the last read post, for later readings
                oldestPostId = child.getName();
            }

            if(child.getName() != "nrOfPosts" && !prevOldestPostId.equals(child.getName()))
            {
                UserPost readPost = new UserPost();

                for(DataSnapshot childLvl2 : child.getChildren()) {
                    if(childLvl2.getName() == "post")
                        readPost.setMessage(childLvl2.getValue().toString());
                    else
                        readPost.setDate(childLvl2.getValue().toString());
                }
                toAdd.add(0, readPost);

            }
        }
        prevOldestPostId = oldestPostId;

        //Insert it to the List for the listView
        for (UserPost aToAdd : toAdd)
        {
            thePosts.add(aToAdd);


            // We need notify the adapter that the data have been changed
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

3. when I pull to the top of the listView, I want to get all the rows that has been made after the row that is now displayed as the first one. (Is that understandable...?)

postQuery.startAt(1, newestPostId).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot child : dataSnapshot.getChildren())
        {
            if(child.getName() != "nrOfPosts")
            {
                UserPost readPost = new UserPost();

                for(DataSnapshot childLvl2 : child.getChildren()) {
                    if(childLvl2.getName() == "post")
                        readPost.setMessage(childLvl2.getValue().toString());
                    else
                        readPost.setDate(childLvl2.getValue().toString());
                }
                //Prevent from get the currently newest post again...
                if(!readPost.getMessage().equals(thePosts.get(0).getMessage()))
                    toAdd.add(readPost);

                //Save the name of the last read post, for later readings
                newestPostId = child.getName();
            }
        }
        for (UserPost aToAdd : toAdd)
        {
            thePosts.add(0, aToAdd);

            // Call onLoadMoreComplete when the LoadMore task, has finished
            mListView.onRefreshComplete();
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

I hope I can be of help to someone atleast.

Upvotes: 8

Related Questions