Sort listview by the amount of Ikes in Firebase

Asked

Viewed 70 times

0

I need my Listview items with more Ikes to appear first in my feed, how do I do that? follows image of firebase structureinserir a descrição da imagem aqui

My Adapter

 @NonNull
@Override
public View getView(final int position, @Nullable View 
 convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = (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) {

        }
    });

1 answer

0

Before passing the list to adpter. You can sort it using the amount of Likes as criteria. This example shows how to implement Comparable, used to sort a List Object. Collections.Sort(myLista), odena your list of items with more Ikes.

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;
}   
  • I don’t quite understand, but thanks for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.