Issue-368 fix transliteration poi, issue-363 update filter after button pressed

This commit is contained in:
Victor Shcherb 2011-04-03 17:11:15 +02:00
parent 3be8cc3298
commit 694c7b7a8c

View file

@ -5,6 +5,7 @@ package net.osmand.activities.search;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection;
import java.util.List; import java.util.List;
import net.osmand.Algoritms; import net.osmand.Algoritms;
@ -58,6 +59,7 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Filter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
@ -129,10 +131,10 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
if(res != null){ if(res != null){
Toast.makeText(SearchPOIActivity.this, res, Toast.LENGTH_LONG).show(); Toast.makeText(SearchPOIActivity.this, res, Toast.LENGTH_LONG).show();
} }
amenityAdapter.setNewModel(((NameFinderPoiFilter) filter).getSearchedAmenities()); amenityAdapter.setNewModel(((NameFinderPoiFilter) filter).getSearchedAmenities(), "");
showOnMap.setEnabled(amenityAdapter.getCount() > 0); showOnMap.setEnabled(amenityAdapter.getCount() > 0);
} else { } else {
amenityAdapter.setNewModel(filter.searchFurther(location.getLatitude(), location.getLongitude())); amenityAdapter.setNewModel(filter.searchFurther(location.getLatitude(), location.getLongitude()), searchFilter.getText().toString());
} }
searchedLocation = location; searchedLocation = location;
@ -199,7 +201,7 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
filter.clearPreviousZoom(); filter.clearPreviousZoom();
} else if(!isNameFinderFilter()) { } else if(!isNameFinderFilter()) {
searchedLocation = location; searchedLocation = location;
amenityAdapter.setNewModel(filter.initializeNewSearch(location.getLatitude(), location.getLongitude(), 40)); amenityAdapter.setNewModel(filter.initializeNewSearch(location.getLatitude(), location.getLongitude(), 40), "");
} }
setListAdapter(amenityAdapter); setListAdapter(amenityAdapter);
searchPOILevel.setEnabled(filter.isSearchFurtherAvailable()); searchPOILevel.setEnabled(filter.isSearchFurtherAvailable());
@ -258,7 +260,7 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
if (location == null) { if (location == null) {
searchedLocation = l; searchedLocation = l;
if (!isNameFinderFilter()) { if (!isNameFinderFilter()) {
amenityAdapter.setNewModel(filter.searchAgain(l.getLatitude(), l.getLongitude())); amenityAdapter.setNewModel(filter.searchAgain(l.getLatitude(), l.getLongitude()), searchFilter.getText().toString());
searchPOILevel.setText(R.string.search_POI_level_btn); searchPOILevel.setText(R.string.search_POI_level_btn);
} else { } else {
searchPOILevel.setText(R.string.search_button); searchPOILevel.setText(R.string.search_button);
@ -267,7 +269,7 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
searchArea.setText(filter.getSearchArea()); searchArea.setText(filter.getSearchArea());
handled = true; handled = true;
} else if (searchedLocation != null && l.distanceTo(searchedLocation) > MIN_DISTANCE_TO_RESEARCH) { } else if (searchedLocation != null && l.distanceTo(searchedLocation) > MIN_DISTANCE_TO_RESEARCH) {
amenityAdapter.setNewModel(filter.searchAgain(l.getLatitude(), l.getLongitude())); amenityAdapter.setNewModel(filter.searchAgain(l.getLatitude(), l.getLongitude()), searchFilter.getText().toString());
handled = true; handled = true;
} else if(location.distanceTo(l) > MIN_DISTANCE_TO_UPDATE){ } else if(location.distanceTo(l) > MIN_DISTANCE_TO_UPDATE){
handled = true; handled = true;
@ -556,19 +558,23 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
} }
} }
class AmenityAdapter extends ArrayAdapter<Amenity> { class AmenityAdapter extends ArrayAdapter<Amenity> {
private AmenityFilter filter;
private List<Amenity> originalAmenityList;
AmenityAdapter(List<Amenity> list) { AmenityAdapter(List<Amenity> list) {
super(SearchPOIActivity.this, R.layout.searchpoi_list, list); super(SearchPOIActivity.this, R.layout.searchpoi_list, list);
originalAmenityList = new ArrayList<Amenity>(list);
this.setNotifyOnChange(false); this.setNotifyOnChange(false);
} }
public void setNewModel(List<Amenity> amenityList) { public void setNewModel(List<Amenity> amenityList, String filter) {
setNotifyOnChange(false); setNotifyOnChange(false);
originalAmenityList = new ArrayList<Amenity>(amenityList);
clear(); clear();
for (Amenity obj : amenityList) { for (Amenity obj : amenityList) {
this.add(obj); add(obj);
} }
getFilter().filter(filter);
setNotifyOnChange(true); setNotifyOnChange(true);
this.notifyDataSetChanged(); this.notifyDataSetChanged();
@ -636,6 +642,47 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
} }
return (row); return (row);
} }
@Override
public Filter getFilter() {
if (filter == null) {
filter = new AmenityFilter();
}
return filter;
}
private final class AmenityFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
results.values = originalAmenityList;
results.count = originalAmenityList.size();
} else {
String lowerCase = constraint.toString()
.toLowerCase();
List<Amenity> filter = new ArrayList<Amenity>();
for (Amenity item : originalAmenityList) {
String lower = item.getStringWithoutType(OsmandSettings.usingEnglishNames(settings)).toLowerCase();
if(lower.indexOf(lowerCase) != -1){
filter.add(item);
}
}
results.values = filter;
results.count = filter.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
for (Amenity item : (Collection<Amenity>) results.values) {
add(item);
}
}
}
} }