Nou*_*l M 2 sorting android listview date
在我的应用程序中,我必须显示一个列表视图,其中显示Date和String值.一个Model类用于向Array列表提供值.Date.Date值是使用Adapter Class中Model类的值派生的.Have显示按日期升序排序的列表视图,并突出显示最接近当前date.highlight的视图.特定视图的背景颜色,其下一个大于或等于当前日期.
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;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我吗?先谢谢
在哪里调用适配器类使用:
Collections.sort(mListItems, new CustomComparator());
adapter = new YourAdater(this,dateOfBirth, mListItems);
lv.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
您的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
}
}
Run Code Online (Sandbox Code Playgroud)
要突出显示前一行或两行,请在适配器的getView方法中编写此代码.无需在此处进行任何排序.
if(position==0){ // for two rows if(position==0 || position==1)
convertView.setBackgroudColor(Your Color);
}
Run Code Online (Sandbox Code Playgroud)
希望它能帮到你.如果您遇到任何问题,请发表评论.
| 归档时间: |
|
| 查看次数: |
12380 次 |
| 最近记录: |