How to sort or reorder MergeCursor for ListViewAdapter

I am developing a simple social media for my case study. I was able to retrieve the post from the people the user follows. Here’s the screenshot:

enter image description here

As you can see, the problem is that the posts were not sorted according to date/id. Instead, it is sorted according to the people the user follows. It is because I am only merging the cursors using mergecursor. Here’s a part of my code:

    ListView listviewFeed = (ListView) findViewById(R.id.listviewFeed);
    Cursor cursorFeed = DataAdapters.getFeed(dbHelper, strUserID);
    //This code is for retrieving user's own posts

    Cursor cursorFollowing = DataAdapters.getFollowing(dbHelper,strUserID);
    //This code is for retrieving the followed users.

    if(cursorFollowing.getCount()>0) {
        for (int intCtr = 0; intCtr < cursorFollowing.getCount(); intCtr++) {
            int intUseridI = cursorFollowing.getColumnIndex(Tables.FollowTable.COLUMN_USERID);
            String strUseridI = cursorFollowing.getString(intUseridI);

            Cursor cursorFollowingFeed = DataAdapters.getFeed(dbHelper, strUseridI);
            This code is for retrieving the posts of the people the user follows.

            if(intCtr>0)
            {
                mergeCursor = new MergeCursor(new Cursor[]{mergeCursor, cursorFollowingFeed});
            }else {
                mergeCursor = new MergeCursor(new Cursor[]{cursorFeed, cursorFollowingFeed});
            }
            //This code is for merging the cursors.

            if (intCtr + 1 == cursorFollowing.getCount()) {
                cursorFollowing.close();
            } else {
                cursorFollowing.moveToNext();
            }
        }
        ListViewAdapterMeasurement adapterMeasurement = new ListViewAdapterMeasurement(this, mergeCursor);
        listviewFeed.setAdapter(adapterMeasurement);
    }else
    {
        ListViewAdapterMeasurement adapterMeasurement = new ListViewAdapterMeasurement(this, cursorFeed);
        listviewFeed.setAdapter(adapterMeasurement);
    }

It is all working well. I just want to order the posts by Date or by ID.

Is there any way to sort MergeCursor?

Source: java

Leave a Reply