Sorry if the question seems to long than normal or difficult to understand .In my task I have to show an listview which will display a date and title as string values.I am using Arraylist which uses a model class for passing the data to adapter.
vaccinationScheduleAdapter=new VaccinationScheduleAdapter(mActivity,DateOfBirth,VaccShdleModelArrayList);
lstMenus.setAdapter(vaccinationScheduleAdapter);
Where DateOfBirth is a DateValue,which can be changed.The value for VaccShdleModelArrayList
is passed through a model class
public String getmDescription() {
return mDescription;
}
public String getmNumberOfMonths() {
return mNumberOfMonths;
}
The date showing in listview will calculated using the passed value DateOfBirth and mNumberOfMonths.the calculation will perform in Custom Adapter class
NumberOfMonths = Integer.parseInt(atrNumberOfMonths);
Calendar cal = Calendar.getInstance();
cal.setTime(DateOfBirth);
cal.add(Calendar.MONTH, NumberOfMonths);
For example the getmNumberOfMonths
will return 1,3,5,7,..
then it will added to month of passed DateOfBirth
.if i passed 01/08/2015
the result after calculation will be 01/09/2015,01/10/2015,01/12/2015,01/03/2015...
I want highlight the next future date if it is not containing current date.
I managed to find the position needed to be highlight by sorting dates coming after current date, but the problem is sorting and finding of happens only when the position of the adapter will be last. please help me to do this. stuck in this problem for a long time.
Here is the Adapter Class,
public class VaccinationScheduleAdapter extends BaseAdapter {
private ArrayList<VaccinationScheduleModel> mListItems;
private Activity mActivity;
private LayoutInflater mInflater;
private Integer[] mListIcons;
private Date DateOfBirth;
private int NumberOfMonths;
private ArrayList<VaccinationScheduleAppoinmentModel> SottList = new ArrayList<>();
VaccinationScheduleAppoinmentModel ap;
CalenderModel calModel = new CalenderModel();
private int maxarraySize;
public static int nextvalue;
public VaccinationScheduleAdapter(Activity mActivity, Date DateOfBirth, ArrayList<VaccinationScheduleModel> mListItems) {
// super();
this.maxarraySize = mListItems.size();
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();
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);
RelativeLayout relrow = (RelativeLayout) convertView.findViewById(R.id.relrow);
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);
Calendar cal2 = Calendar.getInstance();
int comresult = cal.compareTo(cal2);
System.out.println("27102015 cal1 Added date:"+cal);
System.out.println("27102015 cal1 Current date:"+cal2);
// System.out.println("22102015:maxarraySize" + maxarraySize);
// System.out.println("22102015:position" + position);
System.out.println("22102015:comresult"+comresult);
if (position == (maxarraySize - 1)) {
Collections.sort(SottList, new ScheduleDateComp());
System.out.println("Sorted list entries: ");
for (VaccinationScheduleAppoinmentModel e : SottList) {
System.out.println(e);
}
if (SottList.size() != 0) {
int aortlistposition = SottList.get(0).getposition();
System.out.println("26102015:highlight position" + aortlistposition);
System.out.println("26102015:current position" + position);
nextvalue = aortlistposition;
if (position == aortlistposition) {
relrow.setBackgroundResource(R.color.listview_blueback);
}
} else {
}
}
// System.out.println("comresult" + comresult);
if (isSameDay(cal, cal2)) {
// convertView.setBackgroundColor(Color.BLACK);
// calModel.mCaldate = cal;
// calModel.mPosition = position;
System.out.println("isSameDay");
// convertView.setBackgroundResource(R.drawable.list_selector_highlight);
// txtVaccTitle.setTextColor(Color.WHITE);
// txtVaccDay.setTextColor(Color.WHITE);
// txtVaccMonth.setTextColor(Color.WHITE);
// txtVaccYear.setTextColor(Color.WHITE);
}
else if (comresult < 0) {
convertView.setBackgroundColor(Color.TRANSPARENT);
txtVaccTitle.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
txtVaccDay.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
txtVaccMonth.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
txtVaccYear.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
// txtVaccTitle.setTextColor(Color.WHITE);
// txtVaccDay.setTextColor(Color.WHITE);
// txtVaccMonth.setTextColor(Color.WHITE);
// txtVaccYear.setTextColor(Color.WHITE);
}
else {
if ((comresult > 0)) {
ap = new VaccinationScheduleAppoinmentModel(result, position);
SottList.add(ap);
// convertView.setBackgroundColor(Color.BLACK);
convertView.setBackgroundResource(R.drawable.list_selector_highlight);
Collections.sort(SottList, new ScheduleDateComp());
if ((position == SottList.get(0).getposition()) ) {//&& !isSameDay(cal, cal2)
convertView.setBackgroundResource(R.drawable.list_selector_highlight);
txtVaccTitle.setTextColor(Color.WHITE);
txtVaccDay.setTextColor(Color.WHITE);
txtVaccMonth.setTextColor(Color.WHITE);
txtVaccYear.setTextColor(Color.WHITE);
} else {
convertView.setBackgroundColor(Color.TRANSPARENT);
txtVaccTitle.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
txtVaccDay.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
txtVaccMonth.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
txtVaccYear.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
}
// checkNextDay();
}
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return convertView;
}
public int nextday() {
return nextvalue;
}
public boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null)
return false;
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
&& cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
&& cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}
class ScheduleDateComp implements Comparator<VaccinationScheduleAppoinmentModel> {
@Override
public int compare(VaccinationScheduleAppoinmentModel ap1, VaccinationScheduleAppoinmentModel ap2) {
if ((ap1.getDate()).after((ap2.getDate())))
{
return 1;
} else {
return -1;
}
}
}
}
I would be very thankful if any one can be help me. Thanks in Advance.