使用Collections.sort函数。
1 /** 2 * Created by victory on 2016/11/17. 3 * 政务适配器 4 */ 5 6 public class GovernmentAdapter extends ArrayAdapter<GovernmentInfo> { 7 private ArrayList<GovernmentInfo> originalInfo; 8 private Context context; 9 10 public GovernmentAdapter(Context context, ArrayList<GovernmentInfo> items) { 11 super(context, 0, items); 12 this.context = context; 13 this.originalInfo = items; 14 } 15 16 @Override 17 public View getView(int index, View view, ViewGroup arg2) { 18 ViewHoulder houlder = null; 19 if (view == null) { 20 houlder = new ViewHoulder(); 21 view = LayoutInflater.from(context).inflate(R.layout.listitem_government, null); 22 houlder.mtxtActiName = (TextView) view.findViewById(R.id.txtActivityName); 23 houlder.mtxtActivityAddress = (TextView) view.findViewById(R.id.txtActivityAddress); 24 houlder.mtxtStartTime = (TextView) view.findViewById(R.id.txtStartTime); 25 houlder.mtxtLeader = (TextView) view.findViewById(R.id.txtLeader); 26 // houlder.mtxtActivityType = (TextView) view.findViewById(R.id.txtActivityType); 27 houlder.mtxtGovernmentType = (TextView) view.findViewById(R.id.txtGovernmentType); 28 view.setTag(houlder); 29 } else { 30 houlder = (ViewHoulder) view.getTag(); 31 } 32 /** 33 * 字符串比较器,按照时间的升序(o1.getStartTime().compareTo(o2.getStartTime())) 34 * 降序(o2.getStartTime().compareTo(o1.getStartTime())) 35 */ 36 Collections.sort(originalInfo, new Comparator<GovernmentInfo>() { 37 @Override 38 public int compare(GovernmentInfo o1, GovernmentInfo o2) { 39 if (!o1.getStartTime().equals(o2.getStartTime())) { 40 return o2.getStartTime().compareTo(o1.getStartTime()); 41 } 42 return 0; 43 } 44 }); 45 46 GovernmentInfo item = originalInfo.get(index); 47 if (item != null) { 48 houlder.mtxtActiName.setText(item.getTitle()); 49 houlder.mtxtActivityAddress.setText(item.getLocation()); 50 houlder.mtxtStartTime.setText(item.getTime()); 51 // houlder.mtxtActivityType.setText(item.getType()); 52 houlder.mtxtGovernmentType.setText(item.getStatus()); 53 54 houlder.mtxtGovernmentType.setBackgroundResource(R.drawable.tv_circular); 55 /** 56 * 在使用shape的同时,用代码修改shape的颜色属性 57 * 一个view只能设置一个background,每设置一次Background,另一个Background就会就会被替换掉 58 *所以用下面这个函数来进行多个设置 59 */ 60 GradientDrawable myGrad = (GradientDrawable) houlder.mtxtGovernmentType.getBackground(); 61 if (item.getStatus().equals("登记")) { 62 myGrad.setColor(context.getResources().getColor(R.color.theme_color_primary_light)); 63 } else { 64 myGrad.setColor(context.getResources().getColor(R.color.zong)); 65 } 66 List<PeopleInfo> list = item.getLaders(); 67 // houlder.mtxtLeader.setAdapter(new NameAdapter(x.app(), list));String leadernames = null; 68 String leadernames = null; 69 int size = list.size(); 70 for (int i = 0; i < size; i++) { 71 if (i == 0) 72 leadernames = list.get(i).getName(); 73 else 74 leadernames = leadernames + "," + list.get(i).getName(); 75 } 76 if (leadernames != null) { 77 houlder.mtxtLeader.setText(leadernames); 78 } 79 } 80 81 return view; 82 } 83 84 public final class ViewHoulder { 85 private TextView mtxtActiName;//活动名称 86 private TextView mtxtActivityAddress;//活动地点 87 private TextView mtxtStartTime;//开始时间 88 private TextView mtxtLeader;//出席领导 89 // private TextView mtxtActivityType;//活动类型 90 private TextView mtxtGovernmentType;//登记类型 91 } 92 }