简体   繁体   中英

Reorder Recyclerview items with custom adapter

I have a problem that many people had but i don't understand how can i get out of it. Please help me in my case and do not mark as duplicated. I have a recyclerview linked with firebase. My recyclerview has a custom adapter called MyAdapter:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> implements Filterable {

Context c;
ArrayList<Studi> studi;
ArrayList<Studi> filterList;
CustomFilter filter;

public MyAdapter(Context c, ArrayList<Studi> studi) {
    this.c = c;
    this.studi = studi;
    this.filterList = studi;

}


@Override
public MyHolder onCreateViewHolder (ViewGroup parent, int viewType){
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
    MyHolder holder = new MyHolder(v);

    return holder;
}
@Override
public void onBindViewHolder (MyHolder holder, int position){
    String name = studi.get(position).getName();
    String desc = studi.get(position).getDescription();
    String prof = studi.get(position).getProfessione();
    String tel = studi.get(position).getTelefono();
    String coord1 = studi.get(position).getCoordinata1();
    String coord2 = studi.get(position).getCoordinata2();
    String distanza = studi.get(position).getDistanza();
    holder.nameTxt.setText(name);
    holder.descTxt.setText(desc);
    holder.profTxt.setText(prof);
    holder.telTxt.setText(tel);
    holder.pos1Txt.setText(coord1);
    holder.pos2Txt.setText(coord2);
    holder.distanza.setText(distanza);
}

@Override
public int getItemCount (){
    return (studi == null) ? 0 : studi.size();
}

//FILTER THING......................
public Filter getFilter() {
    if(filter == null)
    {
        filter=new CustomFilter(filterList,this);
    }
    return filter;
}}

All the items have got a textview inside where it is written the String Distanza. This string is a number for each recyclerview item. I need to reorder all the recyclerview items from the lower to the higher (Value of the String Distanza).

This is my Studi class which i use for the arraylist:

public class Studi {

private String name;

private String description;
private String professione;
private String telefono;
private String coordinata1;
private String coordinata2;
private String distanza;

public void setDistanza(String distanza) {
    this.distanza = distanza;
}

public String getDistanza() {
    return distanza;
}

public String getCoordinata2() {
    return coordinata2;
}

public void setCoordinata2(String coordinata2) {
    this.coordinata2 = coordinata2;
}

public String getCoordinata1() {
    return coordinata1;
}

public void setCoordinata1(String coordinata1) {
    this.coordinata1 = coordinata1;
}



public String getProfessione() {
    return professione;
}

public void setProfessione(String professione) {
    this.professione = professione;
}

public String getTelefono() {
    return telefono;
}

public void setTelefono(String telefono) {
    this.telefono = telefono;
}

public Studi() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}}

So at the end, i need to reorder them at the top of the recyclerview from the ones that return a getDistanza() value lower than the others. How can i do it? I have really no idea, and sorry but i am new at this.

You can sort your data before passing it to MyAdapter . You can use the sort method from the Collections class. You would have something like this:

Collections.sort(studi, new Comparator<Studi>() {
    public int compare(Studi s1, Studi s2) {
            return s1.getDistanza() - s2.getDistanza();
    }
})

At the end, whenever you filter your data, it always will respect the getDistanza sorting.

You should not sort your data while you are inflating your recyclerView . Infact now that you are already fetching your data from firebase-database, you should use your query accordingly to get the result in sorted form.

If you want your list to be in increasing order of distance (distanza in your case), just use a query something like this

const query = itemsRef.orderByChild('distanza');

This will give you a sorted array which you dont need to sort anymore.

PS If you want your recyclerView to have changes in real-time, do not use your custom adapter, instead use the firebaseRecyclerviewAdapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM