Android Correct way if implementing filterable in Auto-Complete
What i am trying to do is and failing miserably is this:
In my view I have an auto-complete Textview. I would like to fetch via a GET a json array of objects. Takes about 1-2 secs... (Should i use AsyncTask or a Handler to support this fetch?)
Then filter user's input based on this array.
Currently I have implemented my custom adaptor as so...
public class StationAdapter extends BaseAdapter implements Filterable {
Context _ctx;
//returned stations...
ArrayList<Station> _stations;
// to hold original stations...
private ArrayList<Station> orig;
//Custom filter to be used
private final StationFilter filter;
public StationAdapter(final Context ctx, final ArrayList<Station> stations) {
this._ctx = ctx;
this._stations = stations;
this.orig = stations;
this.filter = new StationFilter();
}
@Override
public int getCount() {
if (_stations != null)
return _stations.size();
else
return 0;
}
@Override
public Object getItem(final int position) {
return _stations.get(position);
}
//IS unused? NO whats its real purpose ?...
@Override
public long getItemId(final int position) {
return (position);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
StationView sv;
if (convertView == null)
sv = new Stat开发者_StackOverflowionView(_ctx, _stations.get(position));
else {
sv = (StationView) convertView;
sv.setCode(_stations.get(position).mCode);
sv.setName(_stations.get(position).mName);
}
return sv;
}
@Override
public Filter getFilter() {
return filter;
}
private class StationFilter extends Filter {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Station> results = new ArrayList<Station>();
if (orig == null)
orig = _stations;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final Station g : orig) {
if (g.mName.contains(constraint.toString().toUpperCase()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(final CharSequence constraint, final FilterResults results) {
_stations = (ArrayList<Station>) results.values;
notifyDataSetChanged();
}
}
}
For some reason the filtered response is triggered every other input key my main.xml looks simply like this...
<AutoCompleteTextView android:id="@+id/search_stations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:singleLine="true"
/>
Could someone point out what i am doing wrong and some tutorials that deal with such a use-case
Thanks in advance
This is very late, but I believe that's the expected behavior. The filtering logic will queue the requests while another request is outgoing. So if you type "a" it will go fetch the results and any letter(s) after that will be waiting until the results for "a" come back. After those results come back, a new request will go out, and so on.
salam I found the solution here is the code that you must make
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final station g : orig) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
精彩评论