过滤ArrayList <object>基于两个间隔的android

时间:2015-08-05 10:24:41

标签: android arraylist android-adapter filtered-index

在Arraylist中,如何将arraylist过滤为2个日期之间的行。

例如,

从2015年10月到12月找到所有arraylist的学生。

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);

这是我的工作。如果日期在开始日期和结束日期之间。它必须显示true,否则为

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();

       /* for (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" and "date1" must be converted to dateFormat
            {
                Log.e("valid","true");
                //filteredDate.add(convertedDate); // You can use these filtered ArrayList
                return true;
            }
        //}
        Log.e("valid","false");

        return false;
    }

上述方法用于过滤我的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 the filtered values

                Log.e("c", String.valueOf(mDisplayedValues.size()));
               if (mDisplayedValues.isEmpty()||mDisplayedValues==null){

                    Log.e("FIELD_EMPTY", "NO_RECORD_FOUND");
                }

                notifyDataSetChanged();  // notifies the data with new filtered values
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                ArrayList<Expenses> FilteredArrList = new ArrayList<Expenses>();

                if (mOriginalValues == null) {
                    mOriginalValues = new ArrayList<Expenses>(mDisplayedValues); // saves the original data in mOriginalValues
                }

                /******
                 *
                 *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                 *  else does the Filtering and returns FilteredArrList(Filtered)
                 *
                 *****/
                if (constraint == null || constraint.length() == 0) {

                    // set the Original result to 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();
                    for (int i = 0; i < 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 the Filtered result to return
                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }
                return results;
            }
        };
        return filter;
    }

0 个答案:

没有答案