2017-04-10 26 views
0

我正在開發中,我想從API來的價格進行排序, 1.低到高 2.高至低排序RecyclerView Accending/Decending訂購其價值

的應用程序是在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); 
     } 
    }); 

} 
+0

爲什麼不ü列表進行排序和重新填充回收者視圖? – Moulesh

+0

我怎麼樣,請告訴我@Moulesh –

+0

您想根據價格對您的清單數據進行排序嗎? – Moulesh

回答

2

你應該排序你doctorList列表中,然後調用adapter.notifyDataSetChanged();重新繪製您的RecylerView視圖。

+0

如何排序?@Alex –

+0

使用可比較的界面,查看以下鏈接瞭解更多信息:http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and -comparator / – AlexTa

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; 
} 
}