Filter ArrayList<Object> based on based between two intervals android -
in arraylist, how can 1 filtered arraylist rows between 2 dates.
for example,
find students in arraylist october december 2015.
new student ("12/05/2015",john, level2); new student ("13/05/2015",bisnet, level2); new student ("5/05/2015",tube, level2); new student ("7/05/2015",eros, level2); new student ("4/05/2015",mackay, level2); new student ("8/05/2015",walnet, level2);
here working. if date between start , ending date. must display true else false
public boolean systemdate(date startdate, date enddate, string exps) { simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date expenselsdate=null; try { expenselsdate = sdf.parse(exps.substring(0,10)); } catch (parseexception e) {} // date expenselsdate = datefortmat(exps); //simpledateformat dateformat = new simpledateformat("dd-mmm-yyyy");//dd-mmm-yyyy hh:mm:ss aa // date converteddate = new date(); // arraylist<expenses> filtereddate = new arraylist<>(); // iterator<expenses> iterator = llist.iterator(); /* (expenses temp : llist) { try { converteddate = dateformat.parse(temp.date_of_entry); } catch (exception e) { e.printstacktrace(); }*/ if ((expenselsdate.before(enddate)) && (expenselsdate.after(startdate))) //here "date2" , "date1" must converted dateformat { log.e("valid","true"); //filtereddate.add(converteddate); // can use these filtered arraylist return true; } //} log.e("valid","false"); return false; }
the above method used filter baseadapter.
@override public filter getfilter() { filter filter = new filter() { @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint,filterresults results) { mdisplayedvalues = (arraylist<expenses>) results.values; // has filtered values log.e("c", string.valueof(mdisplayedvalues.size())); if (mdisplayedvalues.isempty()||mdisplayedvalues==null){ log.e("field_empty", "no_record_found"); } notifydatasetchanged(); // notifies data new filtered values } @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); // holds results of filtering operation in values arraylist<expenses> filteredarrlist = new arraylist<expenses>(); if (moriginalvalues == null) { moriginalvalues = new arraylist<expenses>(mdisplayedvalues); // saves original data in moriginalvalues } /****** * * if constraint(charsequence received) null returns moriginalvalues(original) values * else filtering , returns filteredarrlist(filtered) * *****/ if (constraint == null || constraint.length() == 0) { // set original result return results.count = moriginalvalues.size(); results.values = moriginalvalues; } else { /* public expenses(string expenses_id, string date_of_entry, string name, string category_id, string amount, string currency_id, string comments, string fileid, string user_id) */ constraint = constraint.tostring().tolowercase(); (int = 0; < moriginalvalues.size(); i++) { string expname = moriginalvalues.get(i).name; string expamount = moriginalvalues.get(i).amount; string expcomment = moriginalvalues.get(i).comments; // date startdate = systemutils.datefortmat(moriginalvalues.get(i).date_of_entry); if (date1!=null && date2!=null) isdatevalid = systemutils.systemdate(date1,date2,moriginalvalues.get(i).date_of_entry); if (expname.tolowercase().contains(constraint.tostring()) || (expamount.contains(constraint.tostring()) || (expcomment.contains(constraint.tostring())&& isdatevalid))) { filteredarrlist.add( new expenses(moriginalvalues.get(i).expenses_id, moriginalvalues.get(i).date_of_entry, moriginalvalues.get(i).name, moriginalvalues.get(i).category_id, moriginalvalues.get(i).amount, moriginalvalues.get(i).currency_id, moriginalvalues.get(i).comments, moriginalvalues.get(i).fileid, moriginalvalues.get(i).user_id )); } } // set filtered result return results.count = filteredarrlist.size(); results.values = filteredarrlist; } return results; } }; return filter; }
Comments
Post a Comment