按其值排序RecyclerView Accending / Decending Order

时间:2017-04-10 08:36:25

标签: java android android-recyclerview recycler-adapter

我正在开发一个应用程序,我想在其中对来自API的价格进行排序  1.从低到高  2.从高到低

recylerView中的任何方法都可以按升序和降序排列项目。

这是我的适配器OnBindViewHolder方法:

  @Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    typeface = Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-SemiBold.ttf");
    String email = doctorList.get(position).getDoctor().getDoctor_email();
    doctor_id = doctorList.get(position).getDoctor().getDoctor_id();

     holder.dr_name.setText(doctorList.get(position).getDoctor().getDoctor_name());
     holder.dr_exp.setText(doctorList.get(position).getDoctor().getDoctor_exp()+" exp.");
     holder.dr_fee.setText("Rs. "+doctorList.get(position).getDoctor().getDoctor_fee());
     Log.e("FEE",""+doctorList.get(position).getDoctor().getDoctor_fee());

    // holder.dr_imageDL.setImageURI(Uri.parse(doctorList.get(position).getDoctor().getDoctor_img()));
    Picasso.with(context)
            .load(doctorList.get(position).getDoctor().getDoctor_img())
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .resize(200, 200)
            .placeholder(R.drawable.doctor_img)
            .into(holder.dr_imageDL);

    holder.bookBtn.setTypeface(typeface);
    holder.bookBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), BookAppointment.class);
            intent.putExtra("DOCTOR_ID",doctor_id);
            view.getContext().startActivity(intent);
             Log.e("CLICK","CLICK"+doctor_id);
        }
    });

}

3 个答案:

答案 0 :(得分:2)

你应该排序doctorList列表,然后调用adapter.notifyDataSetChanged();重新绘制RecylerView视图。

答案 1 :(得分:2)

你可以使用比较器根据特定值对列表中的对象进行排序,在你的情况下,你可以有两个比较器,如升序和降序..这里你有一个解决方案基于年龄排序列表,你可以实现相同的价格在你的对象中 http://www.javatpoint.com/Comparator-interface-in-collection-framework 你也可以通过单个比较器实现这一点,请检查此链接: https://stackoverflow.com/a/1947527/6350239

答案 2 :(得分:1)

您可以看到此代码以获取帮助

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSort {

public static void main(String args[]){

    ToSort toSort1 = new ToSort(new Float(3), "3");
    ToSort toSort2 = new ToSort(new Float(6), "6");
    ToSort toSort3 = new ToSort(new Float(9), "9");
    ToSort toSort4 = new ToSort(new Float(1), "1");
    ToSort toSort5 = new ToSort(new Float(5), "5");
    ToSort toSort6 = new ToSort(new Float(0), "0");
    ToSort toSort7 = new ToSort(new Float(3), "3");
    ToSort toSort8 = new ToSort(new Float(-3), "-3");

    List<ToSort> sortList = new ArrayList<ToSort>();
    sortList.add(toSort1);
    sortList.add(toSort2);
    sortList.add(toSort3);
    sortList.add(toSort4);
    sortList.add(toSort5);
    sortList.add(toSort6);
    sortList.add(toSort7);
    sortList.add(toSort8);

    Collections.sort(sortList);

    for(ToSort toSort : sortList){
        System.out.println(toSort.toString());
    }
}

}

public class ToSort implements Comparable<ToSort> {

private Float val;
private String id;

public ToSort(Float val, String id){
    this.val = val;
    this.id = id;
}

@Override
public int compareTo(ToSort f) {

    if (val.floatValue() > f.val.floatValue()) {
        return 1;
    }
    else if (val.floatValue() <  f.val.floatValue()) {
        return -1;
    }
    else {
        return 0;
    }

}

@Override
public String toString(){
    return this.id;
}
}