I have a recordset of about 29,000 records. My Screen contains EditText Box for Search Criteria and Listview containing all 29,000 records.
By searching with the listed way it takes time and not giving flow less output as I need.
My EditText contains
final EditText txtSearchCity = (EditText) findViewById(R.id.edtCity);
txtSearchCity.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
aCountryIDTemp.clear();
aCityStateTemp.clear();
for (int i = 0; i < aCountryID.size(); i++) {
if (aCityState
.get(i)
.toLowerCase()
.contains(
txtSearchCity.getText().toString()
.toLowerCase())) {
aCountryIDTemp.add(aCountryID.get(i));
aCityStateTemp.add(aCityState.get(i));
}
}
BindList();
}
});
}
BindList() method is setting the arraylist aCityStateTemp to adapter. Any Other way to Search and Create new ArrayList dynamically.
I would insist to use Lambdaj Library which is mostly used in such cases where you want to restrict loops for sorting and filtering Collections.
Here is a small example for using lambdaj for filtering ArrayList
.
ArrayList<String> sortedArrayList = select(arrList, having(on(String.class),
Matchers.containsString("a");
This will return a complete filtered ArrayList
with which you want to populate your ListView
.
You can also filter
Custom Classes - Java: What is the best way to filter a Collection?
UPDATE:
Above solution was case-sensitive
so to work around you can add Multiple Matchers.
Like this you can add Multiple Matchers
,
ArrayList<String> sortedArrayList = select(arrList, having(on(String.class),
(Matchers.anyOf(Matchers.containsString("a"),Matchers.containsString("A")))));
UPDATE:
Even better way is to use filter(Matcher<?> matcher, T...array)
Here is how you can do that,
ArrayList<String> sortedArrayList = filter(Matchers.anyOf(
Matchers.containsString("a"),Matchers.containsString("A")), arrList);
Also, if you are interested in using some of the methods/features of lambdaj
, you can extract the source and get it working. I am adding the same for filter()
You can just download hamcrest-all-1.0.jar(63 kb)
and add below code to get the filter()
working
public static <T> List<T> filter(Matcher<?> matcher, Iterable<T> iterable) {
if (iterable == null)
return new LinkedList<T>();
else{
List<T> collected = new LinkedList<T>();
Iterator<T> iterator = iterable.iterator();
if (iterator == null)
return collected;
while (iterator.hasNext()) {
T item = iterator.next();
if (matcher.matches(item))
collected.add(item);
}
return collected;
}
}
So, you can just sort out the least from lambdaj
source and integrate in your source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With