firebaserecycleradapter sort/compare/reorder data before populate












0















In my firebase database I have a collection of lat and long for each uid.



Here is how it looks



I use firebase recyler adapter data to list down my data to the recycler view.



FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.row, ViewHolder.class, mRef) {

@Override
protected void populateViewHolder(final ViewHolder viewHolder, final Model model, int position) {

Double dist = distance(
myPosition.latitude,
myPosition.longitude,
Double.parseDouble(model.getLat()),
Double.parseDouble(model.getLon()));
Log.d("gs_distance", String.valueOf(dist));

viewHolder.setDetails(getApplicationContext(),
model.getLat(),
model.getLon(),
model.getGsname());
}


The problem is I want to reorder/sort the data by nearest to farthest from my location, i can already fetch distance on how far each latlng is from my location. I dont know if there is a way to compare each latlng by their distance just before the data enters the populateViewHolder. Or maybe sort it after being inserted in the recyclerview? I really dont know.



static double distance(double fromLat, double fromLon, double toLat, double toLon) {
Location loc1 = new Location("");
loc1.setLatitude(fromLat);
loc1.setLongitude(fromLon);

Location loc2 = new Location("");
loc2.setLatitude(toLat);
loc2.setLongitude(toLon);

float distanceInMeters = loc1.distanceTo(loc2);
return distanceInMeters / 1000; //convert to km
}









share|improve this question




















  • 3





    Firebase adapters from FirebaseUI will show the data in the order in which it is retrieved from he database. I don't think it has support for reordering the items client-side. That means you'll have to write a custom adapter for it, which reads the items from the database, and orders them by distance, and then shows them in that order.

    – Frank van Puffelen
    Nov 14 '18 at 21:32













  • better use your own adapter, because it will give you more freedom in so many aspects

    – Har Kal
    Nov 14 '18 at 22:56











  • Does custom adapter only works with array lists? Im really new at this. Thanks for the reply btw, it gives me motivation to continue with my problem.

    – Mikhail Craox
    Nov 15 '18 at 6:25











  • Should I use dataSnapshot to loop through my firebase data and add each into an arrayList, then from array list into the custom adapter?

    – Mikhail Craox
    Nov 15 '18 at 6:52
















0















In my firebase database I have a collection of lat and long for each uid.



Here is how it looks



I use firebase recyler adapter data to list down my data to the recycler view.



FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.row, ViewHolder.class, mRef) {

@Override
protected void populateViewHolder(final ViewHolder viewHolder, final Model model, int position) {

Double dist = distance(
myPosition.latitude,
myPosition.longitude,
Double.parseDouble(model.getLat()),
Double.parseDouble(model.getLon()));
Log.d("gs_distance", String.valueOf(dist));

viewHolder.setDetails(getApplicationContext(),
model.getLat(),
model.getLon(),
model.getGsname());
}


The problem is I want to reorder/sort the data by nearest to farthest from my location, i can already fetch distance on how far each latlng is from my location. I dont know if there is a way to compare each latlng by their distance just before the data enters the populateViewHolder. Or maybe sort it after being inserted in the recyclerview? I really dont know.



static double distance(double fromLat, double fromLon, double toLat, double toLon) {
Location loc1 = new Location("");
loc1.setLatitude(fromLat);
loc1.setLongitude(fromLon);

Location loc2 = new Location("");
loc2.setLatitude(toLat);
loc2.setLongitude(toLon);

float distanceInMeters = loc1.distanceTo(loc2);
return distanceInMeters / 1000; //convert to km
}









share|improve this question




















  • 3





    Firebase adapters from FirebaseUI will show the data in the order in which it is retrieved from he database. I don't think it has support for reordering the items client-side. That means you'll have to write a custom adapter for it, which reads the items from the database, and orders them by distance, and then shows them in that order.

    – Frank van Puffelen
    Nov 14 '18 at 21:32













  • better use your own adapter, because it will give you more freedom in so many aspects

    – Har Kal
    Nov 14 '18 at 22:56











  • Does custom adapter only works with array lists? Im really new at this. Thanks for the reply btw, it gives me motivation to continue with my problem.

    – Mikhail Craox
    Nov 15 '18 at 6:25











  • Should I use dataSnapshot to loop through my firebase data and add each into an arrayList, then from array list into the custom adapter?

    – Mikhail Craox
    Nov 15 '18 at 6:52














0












0








0








In my firebase database I have a collection of lat and long for each uid.



Here is how it looks



I use firebase recyler adapter data to list down my data to the recycler view.



FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.row, ViewHolder.class, mRef) {

@Override
protected void populateViewHolder(final ViewHolder viewHolder, final Model model, int position) {

Double dist = distance(
myPosition.latitude,
myPosition.longitude,
Double.parseDouble(model.getLat()),
Double.parseDouble(model.getLon()));
Log.d("gs_distance", String.valueOf(dist));

viewHolder.setDetails(getApplicationContext(),
model.getLat(),
model.getLon(),
model.getGsname());
}


The problem is I want to reorder/sort the data by nearest to farthest from my location, i can already fetch distance on how far each latlng is from my location. I dont know if there is a way to compare each latlng by their distance just before the data enters the populateViewHolder. Or maybe sort it after being inserted in the recyclerview? I really dont know.



static double distance(double fromLat, double fromLon, double toLat, double toLon) {
Location loc1 = new Location("");
loc1.setLatitude(fromLat);
loc1.setLongitude(fromLon);

Location loc2 = new Location("");
loc2.setLatitude(toLat);
loc2.setLongitude(toLon);

float distanceInMeters = loc1.distanceTo(loc2);
return distanceInMeters / 1000; //convert to km
}









share|improve this question
















In my firebase database I have a collection of lat and long for each uid.



Here is how it looks



I use firebase recyler adapter data to list down my data to the recycler view.



FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.row, ViewHolder.class, mRef) {

@Override
protected void populateViewHolder(final ViewHolder viewHolder, final Model model, int position) {

Double dist = distance(
myPosition.latitude,
myPosition.longitude,
Double.parseDouble(model.getLat()),
Double.parseDouble(model.getLon()));
Log.d("gs_distance", String.valueOf(dist));

viewHolder.setDetails(getApplicationContext(),
model.getLat(),
model.getLon(),
model.getGsname());
}


The problem is I want to reorder/sort the data by nearest to farthest from my location, i can already fetch distance on how far each latlng is from my location. I dont know if there is a way to compare each latlng by their distance just before the data enters the populateViewHolder. Or maybe sort it after being inserted in the recyclerview? I really dont know.



static double distance(double fromLat, double fromLon, double toLat, double toLon) {
Location loc1 = new Location("");
loc1.setLatitude(fromLat);
loc1.setLongitude(fromLon);

Location loc2 = new Location("");
loc2.setLatitude(toLat);
loc2.setLongitude(toLon);

float distanceInMeters = loc1.distanceTo(loc2);
return distanceInMeters / 1000; //convert to km
}






android firebase firebase-realtime-database firebaseui






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:30









Frank van Puffelen

231k28378402




231k28378402










asked Nov 14 '18 at 21:01









Mikhail CraoxMikhail Craox

11




11








  • 3





    Firebase adapters from FirebaseUI will show the data in the order in which it is retrieved from he database. I don't think it has support for reordering the items client-side. That means you'll have to write a custom adapter for it, which reads the items from the database, and orders them by distance, and then shows them in that order.

    – Frank van Puffelen
    Nov 14 '18 at 21:32













  • better use your own adapter, because it will give you more freedom in so many aspects

    – Har Kal
    Nov 14 '18 at 22:56











  • Does custom adapter only works with array lists? Im really new at this. Thanks for the reply btw, it gives me motivation to continue with my problem.

    – Mikhail Craox
    Nov 15 '18 at 6:25











  • Should I use dataSnapshot to loop through my firebase data and add each into an arrayList, then from array list into the custom adapter?

    – Mikhail Craox
    Nov 15 '18 at 6:52














  • 3





    Firebase adapters from FirebaseUI will show the data in the order in which it is retrieved from he database. I don't think it has support for reordering the items client-side. That means you'll have to write a custom adapter for it, which reads the items from the database, and orders them by distance, and then shows them in that order.

    – Frank van Puffelen
    Nov 14 '18 at 21:32













  • better use your own adapter, because it will give you more freedom in so many aspects

    – Har Kal
    Nov 14 '18 at 22:56











  • Does custom adapter only works with array lists? Im really new at this. Thanks for the reply btw, it gives me motivation to continue with my problem.

    – Mikhail Craox
    Nov 15 '18 at 6:25











  • Should I use dataSnapshot to loop through my firebase data and add each into an arrayList, then from array list into the custom adapter?

    – Mikhail Craox
    Nov 15 '18 at 6:52








3




3





Firebase adapters from FirebaseUI will show the data in the order in which it is retrieved from he database. I don't think it has support for reordering the items client-side. That means you'll have to write a custom adapter for it, which reads the items from the database, and orders them by distance, and then shows them in that order.

– Frank van Puffelen
Nov 14 '18 at 21:32







Firebase adapters from FirebaseUI will show the data in the order in which it is retrieved from he database. I don't think it has support for reordering the items client-side. That means you'll have to write a custom adapter for it, which reads the items from the database, and orders them by distance, and then shows them in that order.

– Frank van Puffelen
Nov 14 '18 at 21:32















better use your own adapter, because it will give you more freedom in so many aspects

– Har Kal
Nov 14 '18 at 22:56





better use your own adapter, because it will give you more freedom in so many aspects

– Har Kal
Nov 14 '18 at 22:56













Does custom adapter only works with array lists? Im really new at this. Thanks for the reply btw, it gives me motivation to continue with my problem.

– Mikhail Craox
Nov 15 '18 at 6:25





Does custom adapter only works with array lists? Im really new at this. Thanks for the reply btw, it gives me motivation to continue with my problem.

– Mikhail Craox
Nov 15 '18 at 6:25













Should I use dataSnapshot to loop through my firebase data and add each into an arrayList, then from array list into the custom adapter?

– Mikhail Craox
Nov 15 '18 at 6:52





Should I use dataSnapshot to loop through my firebase data and add each into an arrayList, then from array list into the custom adapter?

– Mikhail Craox
Nov 15 '18 at 6:52












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308643%2ffirebaserecycleradapter-sort-compare-reorder-data-before-populate%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308643%2ffirebaserecycleradapter-sort-compare-reorder-data-before-populate%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong