how to loop through gridview android -
i creating calendar in android uses gridview, have listview contains selected dates. need have compare each data every item listview gridview. how do it?
here adapter:
public class calendaradapter extends baseadapter { private context mcontext; private java.util.calendar month; public gregoriancalendar pmonth; // calendar instance previous month /** * calendar instance previous month getting complete view */ public gregoriancalendar pmonthmaxset; private gregoriancalendar selecteddate; int firstday; int maxweeknumber; int maxp; int calmaxp; int lastweekday; int leftdays; int mnthlength; string itemvalue, curentdatestring; dateformat df; private arraylist<string> items; public static list<string> daystring; private view previousview; public calendaradapter(context c, gregoriancalendar monthcalendar) { calendaradapter.daystring = new arraylist<string>(); locale.setdefault( locale.us ); month = monthcalendar; selecteddate = (gregoriancalendar) monthcalendar.clone(); mcontext = c; month.set(gregoriancalendar.day_of_month, 1); this.items = new arraylist<string>(); df = new simpledateformat("yyyy-mm-dd", locale.us); curentdatestring = df.format(selecteddate.gettime()); refreshdays(); } public void setitems(arraylist<string> items) { (int = 0; != items.size(); i++) { if (items.get(i).length() == 1) { items.set(i, "0" + items.get(i)); } } this.items = items; } public int getcount() { return daystring.size(); } public object getitem(int position) { return daystring.get(position); } public long getitemid(int position) { return 0; } // create new view each item referenced adapter public view getview(int position, view convertview, viewgroup parent) { view v = convertview; textview dayview; if (convertview == null) { // if it's not recycled, initialize // attributes layoutinflater vi = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); v = vi.inflate(r.layout.calendar_item, null); } dayview = (textview) v.findviewbyid(r.id.date); // separates daystring parts. string[] separatedtime = daystring.get(position).split("-"); // taking last part of date. ie; 2 2012-12-02 string gridvalue = separatedtime[2].replacefirst("^0*", ""); // checking whether day in current month or not. if ((integer.parseint(gridvalue) > 1) && (position < firstday)) { // setting offdays white color. dayview.settextcolor(color.white); dayview.setclickable(false); dayview.setfocusable(false); } else if ((integer.parseint(gridvalue) < 7) && (position > 28)) { dayview.settextcolor(color.white); dayview.setclickable(false); dayview.setfocusable(false); } else { // setting curent month's days in blue color. dayview.settextcolor(color.blue); } if (daystring.get(position).equals(curentdatestring)) { setselected(v); previousview = v; } else { v.setbackgroundresource(r.drawable.list_item_background); } dayview.settext(gridvalue); // create date string comparison string date = daystring.get(position); if (date.length() == 1) { date = "0" + date; } string monthstr = "" + (month.get(gregoriancalendar.month) + 1); if (monthstr.length() == 1) { monthstr = "0" + monthstr; } // show icon if date not empty , exists in items array imageview iw = (imageview) v.findviewbyid(r.id.date_icon); if (date.length() > 0 && items != null && items.contains(date)) { iw.setvisibility(view.visible); } else { iw.setvisibility(view.invisible); } return v; } public view setselected(view view) { if (previousview != null) { previousview.setbackgroundresource(r.drawable.list_item_background); } previousview = view; view.setbackgroundresource(r.drawable.calendar_cel_selectl); return view; } public void refreshdays() { // clear items items.clear(); daystring.clear(); locale.setdefault( locale.us ); pmonth = (gregoriancalendar) month.clone(); // month start day. ie; sun, mon, etc firstday = month.get(gregoriancalendar.day_of_week); // finding number of weeks in current month. maxweeknumber = month.getactualmaximum(gregoriancalendar.week_of_month); // allocating maximum row number gridview. mnthlength = maxweeknumber * 7; maxp = getmaxp(); // previous month maximum day 31,30.... calmaxp = maxp - (firstday - 1);// calendar offday starting 24,25 ... /** * calendar instance getting complete gridview including 3 * month's (previous,current,next) dates. */ pmonthmaxset = (gregoriancalendar) pmonth.clone(); /** * setting start date previous month's required date. */ pmonthmaxset.set(gregoriancalendar.day_of_month, calmaxp + 1); /** * filling calendar gridview. */ (int n = 0; n < mnthlength; n++) { itemvalue = df.format(pmonthmaxset.gettime()); pmonthmaxset.add(gregoriancalendar.date, 1); daystring.add(itemvalue); } } private int getmaxp() { int maxp; if (month.get(gregoriancalendar.month) == month .getactualminimum(gregoriancalendar.month)) { pmonth.set((month.get(gregoriancalendar.year) - 1), month.getactualmaximum(gregoriancalendar.month), 1); } else { pmonth.set(gregoriancalendar.month, month.get(gregoriancalendar.month) - 1); } maxp = pmonth.getactualmaximum(gregoriancalendar.day_of_month); return maxp; } }
and here main activity click on date:
gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { ((calendaradapter) parent.getadapter()).setselected(v); globalvariables.selectedgriddate = calendaradapter.daystring .get(position); string[] separatedtime = globalvariables.selectedgriddate.split("-"); string gridvaluestring = separatedtime[2].replacefirst("^0*", "");// taking last part of date. ie; 2 2012-12-02. int gridvalue = integer.parseint(gridvaluestring); // navigate next or previous month on clicking offdays. if ((gridvalue > 10) && (position < 8)) { setpreviousmonth(); refreshcalendar(); } else if ((gridvalue < 7) && (position > 28)) { setnextmonth(); refreshcalendar(); } ((calendaradapter) parent.getadapter()).setselected(v); requestconsult(globalvariables.selectedgriddate); } }); }
thanks in advance!
you have getitem , getcount. iterate items. this:
for(int i=0;i<adapter.getcount();i++){ adapter.getitem(i).....
Comments
Post a Comment