Android - Sorting documents through iteration of query results before sending them to ListView

Hello,

I woule like to sort the results (Documents) of a guery inside an adapter before sending them to listView.
For the moment it works well, i do this in my adapter :

public class LiveQueryAdapter extends BaseAdapter {

public String TAG = "LiveQueryAdapter";
private LiveQuery query;
private QueryEnumerator enumerator;
private Context context;

int i;

public LiveQueryAdapter(Context context, LiveQuery query) {
    this.context = context;
    this.query = query;

    query.addChangeListener(new LiveQuery.ChangeListener() {
        @Override
        public void changed(final LiveQuery.ChangeEvent event) {
             ((Activity) LiveQueryAdapter.this.context).runOnUiThread(new Runnable() {
             @Override
              public void run() {

                   enumerator = event.getRows();
                   notifyDataSetChanged();
            }
    });
}
});

query.start();

Now let’s say that my results (Documents) contain a value String TimeUpdated (DateTime class).

I want to compare TimeUpdated with actual time, and then sort them descending and send them to my listview.

I think i have to do this in an iteration before calling notifyDataSetChanged();.

Can anyone help me with this ?

Best regards

Hi @heretyk,

In the map function you could emit the TimeUpdated property as well and use the start key and end key query options to filter out the documents you won’t need (you can set the start key to the current time for example).

Can you paste your map/reduce block? Also, how does the comparison of TimeUpdated with the current time change the results? Do you remove the documents with a TimeUpdated that’s prior to the current time?

James

Hi @jamiltz,

Sorry for the late feedback, lot of work ;).

I managed to solve my issue in using a comparator inside my adapter.
I sort the results of query, add them in ordre in a HashMap, and then i inflate views from that HashMap.

I think i couldn’t have used your suggestion, as i am already filtering with keys in that query :

Query query = view.createQuery();

java.util.List<Object> keys = new ArrayList<>();
    keys.add(xxxx1);
    keys.add(xxxx2);
    keys.add(xxxx3);

query.setKeys(keys)

Regards,