How can I filter data from an ArrayList? for example, I have one class called "Date Names". I wrote a little from the code below for my explanation:
public class DateAndNames { int day; int month; int year; String name; public DateAndNames(int day, int month, int year, String name) { super(); this.day = day; this.month = month; this.year = year; this.name = name; } public int getDay() { return day; } ...getters and setters...
and I will write to the database as follows:
DbHandler hand = new DbHandler(this); hand.add(new DateAndNames(20, 3, 2008, "Jhon")); hand.add(new DateAndNames(10, 3, 2008, "Jhon")); hand.add(new DateAndNames(10, 2, 2004, "Jhon")); hand.add(new DateAndNames(22, 3, 2008, "Jhon"));
and then I get the data in an ArrayList as follows:
ArrayList<DateAndNames> list = new ArrayList<DateAndNames>(); list = hand.getData();
and before I pass the list to the BaseAdapter, I want to filter it, so now I do the following:
//filter by month and year: public ArrayList<DateAndNames> filterTheList(int month , int year){ //the data from the database list = hand.getData(); //temp list to store the filtered list ArrayList<DateAndNames> filteredList = new ArrayList<DateAndNames>(); for (int i = 0; i < list.size(); i++) { //check: if(list.get(i).getMonth() == month && list.get(i).getYear() == year){ DateAndNames data = new DateAndNames( list.get(i).getDay(), list.get(i).getMonth(), list.get(i).getYear(), list.get(i).getName()); //The data filtered: filteredList.add(data); } } return filteredList; }
now the big problem: when I have very very big data to run in a for loop, like 300 lines for filtering, the application is very slow! even if you use asyncTask, it is still slow! i'm a little newbie but i would like some good advice
Edited: I also tried this.
public ArrayList<DateAndNames> getData(int month ,int year,String name){ open(); ArrayList<DateAndNames> list = new ArrayList<DateAndNames>(); Cursor c = myDb.query(TABLE_DAY, null, "name= ? and month = ? and year = ?", new String[] {name,month+"",year+""}, null, null, null); while (c.moveToNext()) { DateAndNames resultData = new DateAndNames( c.getInt(0),
But still not working.