Sort listview by the number of likes in Firebase

0

I need items in my ListView with more likes to appear first in my feed, how do I do this? follows image of firebase structure

MyAdapter

@NonNull@OverridepublicViewgetView(finalintposition,@NullableViewconvertView,@NonNullViewGroupparent){LayoutInflaterinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);autenticacao=FirebaseAuth.getInstance();@SuppressLint("ViewHolder") View v = 
    inflater.inflate(R.layout.image_item, parent, false);

    imgList = new ArrayList<>();

    mDatabaseRef = 
    FirebaseDatabase.getInstance().getReference("videos/");

    Query myTopPostsQuery = mDatabaseRef.orderByChild("videos");



    myTopPostsQuery.addValueEventListener(new ValueEventListener() 
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //Fetch image data from firebase database
            for (DataSnapshot snapshot : 
          dataSnapshot.getChildren()) {

                VideoUpload video = 
       snapshot.getValue(VideoUpload.class);
                string = video.getUrl();


            }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    
asked by anonymous 28.10.2018 / 21:53

1 answer

0

Before passing the list to adpter. You can sort it by using as criteria the amount of likes. This example shows with implementing Comparable, which is used to sort a List object. Collections.sort (myLista), edit your list with the most likes item.

public class User implements Comparable<User>{
private String nome;
private int quantLike;

User(String nome, int idade) {
    this.nome = nome;
    this.quantLike = idade;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public int getQuantLike() {
    return quantLike;
}

public void setQuantLike(int quantLike) {
    this.quantLike = quantLike;
}

 public int compareTo(User u) {
    if (getQuantLike() > u.getQuantLike()) {
        return -1;
    }
    if (getQuantLike() < u.getQuantLike()) {
        return 1;
    }
    return 0;
}   
    
04.11.2018 / 22:34