How to sort the CursorLoader results?

前端 未结 4 1519
走了就别回头了
走了就别回头了 2021-01-05 07:04

I use CursorLoader to query a result, which is not the order that I want to show in the ListFramgenet. How to sort it ?

I use this to set the adapter:



        
4条回答
  •  盖世英雄少女心
    2021-01-05 07:48

    It's actually quite easy:

    from this:

    new CursorLoader(getActivity(), 
            Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
            MEMBERS_PROJECTION,
            null,
            null,
            null);
    

    to this:

    // You could have them calculated in the projection like this:
    String[] projection = { COLUMN1 + " * " + COLUMN2 + " as data", // Whatever your calculations are
    COLUMN1, COLUMN2, COLUMN3, etc..  };
    new CursorLoader(getActivity(), 
            Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
            projection, 
            null,
            null,
            "data ASC");
    

    Remember that if you have some method in your provider that does a check to the projection and rise an exception, you would have to comment it out for the moment you are doing the test or add the new column (the one you do the calculation with) to your official projection array.

提交回复
热议问题