I am using a custom adapter to populate a ListView
.
Data are retrieved with a Query i make to a distant DB.
I do an iteration inside the custom adapter of the query's results and then, each object is sent to the the listview using notifyDataSetChanged()
The objects contain a value String TimeUpdated (DateTime class).
I would like to sort the object descending in my listview depending of the TimeUpdated value.
From what i have read, i have to do this inside my iteration before calling 'notifyDataSetChanged();' inside my adapter.
But i don't know how to do this.
If my understanding is right, could someone share sample code to do this ?
Regards to the community
UPDATE : 08/10/2015
I think i have to create an arrayList in my adapter from the iteration of my query's results.
So i did this : 1.I iterate the results of my query and place each object in a arrayList. So i have an arraylist containing Objects. 2.Each objects has got a String value containing "Latitude, Longitude".
I want to sort them from their distance from me.
Calculation of distance works already.
I can't figure out a way to sort the list now ?
private void sortMyList(List<Document> resultsArray) {
Collections.sort(resultsArray, new Comparator<Document>() {
@Override
public int compare(Document doc1, Document doc2) {
String DistanceDoc1 = Double.toString(calculateDistance(location.getLongitude(),location.getLatitude(), getDocLng(doc1), getDocLat(doc1)));
String DistanceDoc2 = Double.toString(calculateDistance(location.getLongitude(),location.getLatitude(), getDocLng(doc2), getDocLat(doc2)));
return DistanceDoc1.compareToIgnoreCase(DistanceDoc2);
}
});
notifyDataSetChanged();
}
Important :
I use two adapter to construct the ListView with the results of my Query.
A parent called LiveQueryAdapter
, that extends from BaseAdapter
.
And the one inside a Fragment called DocumentAdapter
...with extends from LiveQueryAdapter
LiveQueryAdapter Class
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();
***I am thinking about iterate and sort objects here...***
notifyDataSetChanged();
}
});
}
});
query.start();
}
@Override
public void notifyDataSetChanged() {
//do your sorting here
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return enumerator != null ? enumerator.getCount() : 0;
}
@Override
public Object getItem(int i) {
return enumerator != null ? enumerator.getRow(i).getDocument() : null;
}
@Override
public long getItemId(int i) {
return enumerator.getRow(i).getSequenceNumber();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
public void invalidate() {
if (query != null)
query.stop();
}
}
DocumentAdapter Class (inside class of my Fragment)
private class DocumentAdapter extends LiveQueryAdapter {
public DocumentAdapter(Context context, LiveQuery matchQuery) {
super(context, matchQuery);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_item_matches_fragment, null);
}
final Document doc = (Document) getItem(position);
Log.i(TAG, "doc is : " + doc.getProperty("userId"));
if (doc == null || doc.getCurrentRevision() == null) {
return convertView;
}
HERE I DO SET VIEWS IN MY LAYOUT (TEXT & PICTURES)
return convertView;
}
}
In my Fragment :
This adapter is called in onCreateView()
of my Fragment like this :
mAdapter = new DocumentAdapter(getActivity(), matchQuery);
// Set the adapter
mListView = (ListView) view.findViewById(R.id.listview_matches);
mListView.setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
mListView.setOnItemClickListener(this);