如何按日期排序ListView项目
在我的应用程序中,我必须显示一个列表视图,其中显示Date和一个String值。一个Model类用于向Array列表提供值。 Date.Date值是使用适配器类中的Model类的值派生的。具有显示按日期升序排序的列表视图,并突出显示与当前日期最近的视图。下一个大于或等于当前日期的特定视图的背景颜色。如何按日期排序ListView项目
BaseAdapter类
private ArrayList<Model> mListItems;
private Activity mActivity;
private LayoutInflater mInflater;
private Integer[] mListIcons;
private Date DateOfBirth;
private int NumberOfMonths;
private ArrayList<Integer> SottList;
List<Date> dateList = new ArrayList<Date>();
public Adapter(Activity mActivity, Date DateOfBirth,ArrayList<VaccinationScheduleModel> mListItems) {
// super();
this.mActivity = mActivity;
this.mListItems=mListItems;
this.DateOfBirth=DateOfBirth;
mInflater = (LayoutInflater) mActivity.getLayoutInflater();
}
@Override
public int getCount() {
return mListItems.size();
}
@Override
public Object getItem(int position) {
return mListItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_lv_vacc_schedule,
parent, false);
} else {
convertView = convertView;
}
String atrNumberOfMonths=mListItems.get(position).getmNumberOfMonths();
NumberOfMonths = Integer.parseInt(atrNumberOfMonths);
Calendar cal = Calendar.getInstance();
cal.setTime(DateOfBirth);
cal.add(Calendar.MONTH, NumberOfMonths);
Date result = cal.getTime();
dateList.add(result);
String dayOfTheWeek = (String) android.text.format.DateFormat.format("EEEE", result);//Thursday
String stringMonth = (String) android.text.format.DateFormat.format("MMM", result); //Jun
String intMonth = (String) android.text.format.DateFormat.format("MM", result); //06
String year = (String) android.text.format.DateFormat.format("yyyy", result); //2013
String day = (String) android.text.format.DateFormat.format("dd", result); //20
System.out.println("19102015:VaccinationScheduleAdapter" + result);
TextView txtVaccTitle = (TextView) convertView.findViewById(R.id.txtVaccTitle);
TextView txtVaccMonth = (TextView) convertView.findViewById(R.id.txtVaccMonth);
TextView txtVaccDay= (TextView) convertView.findViewById(R.id.txtVaccDay);
TextView txtVaccYear = (TextView) convertView.findViewById(R.id.txtVaccYear);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
imgIcon.setBackgroundResource(R.drawable.calendar_icon);
txtVaccTitle.setText(mListItems.get(position).getmDescription());
txtVaccMonth.setText(stringMonth);
txtVaccDay.setText(day);
txtVaccYear.setText(year);
Collections.sort(dateList);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return convertView;
}
任何一个可以帮助我在此先感谢
如果您的问题仅与排序列表,你可以使用一个Comparator
:
Collections.sort(mListItems, new MyComparator<Model>() {
int compare(Model first, Model second) {
return first.getDate().compareTo(second.getDate());
}
});
它如果您发布用于获取物品清单和您的类的代码将会很有帮助。
上getView
把Collections.sort(dateList)
是完全错误的,你应该生成你的适配器上排序方法和使用它的适配器(也许在片段在onStart或的onResume)确认后,有权要求notifyDataSetChanged
适配器上之外。
如果你打电话给你的适配器类用途:
Collections.sort(mListItems, new CustomComparator());
adapter = new YourAdater(this,dateOfBirth, mListItems);
lv.setAdapter(adapter);
你CustomComparator可能看起来像这样:
public class CustomComparator implements Comparator<YourObject> {// may be it would be Model
@Override
public int compare(YourObject obj1, YourObject obj2) {
return obj1.getDate().compareTo(obj2.getDate());// compare two objects
}
}
为了突出自己的第一个或两行,写你的适配器的getView方法,这里面的代码。无需在这里排序。
if(position==0){ // for two rows if(position==0 || position==1)
convertView.setBackgroudColor(Your Color);
}
希望它可以帮助你。 如果您遇到任何问题,请点评。
这部影片帮了我很多按日期排序:
https://www.youtube.com/watch?v=iTGtk1AuQCI
它是关于如何排序但for
语句。
我首先有一个arrayList
的claseRenta
项目,并且它们每个都包含String
中的日期。我首先将字符串转换为日期,然后在millis中获得时间,然后比较毫秒中的时间并重新排列arrayRentas
。最后,我获得按照每个日期排序的claseRenta
项目的数组。
public void sortRentas() {
SimpleDateFormat format = new SimpleDateFormat("E, d MMM, yyyy hh:mm a");
for(int i=0; i<arrayRentas.size();i++){
for(int j=i+1; j<arrayRentas.size(); j++){
Date date1 = null;
try {
date1 = format.parse(arrayRentas.get(i).getFechaEntrega());
} catch (ParseException e) {
e.printStackTrace();
}
calendario.setTime(date1);
millitime1 = calendario.getTimeInMillis();
Date date2 = null;
try {
date2 = format.parse(arrayRentas.get(j).getFechaEntrega());
} catch (ParseException e) {
e.printStackTrace();
}
calendario.setTime(date2);
millitime2 = calendario.getTimeInMillis();
if(millitime1>millitime2){
temporalRenta = arrayRentas.get(i);
arrayRentas.set(i,arrayRentas.get(j));
arrayRentas.set(j,temporalRenta);
}
}
}
如果你是从获取数据库中的数据,然后做出我不是你在使用使用数据库 –
查询? 从哪里得到日期? –
是 –