有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Sort RecyclerView按其值的收尾/下移顺序

我正在开发一个应用程序,我想排序的价格从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) 个答案

  1. # 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;
    }
    }
    
  2. # 2 楼答案

    您应该对doctorList列表进行排序,然后调用适配器。notifyDataSetChanged();重新绘制RecylerView视图