What I'm trying to do is unsuccessful:
In my opinion, I have AutoFill Textview. I would like to get an array of json objects through GET. Takes about 1-2 seconds ... (Should I use AsyncTask or Handler to support this fetch?)
Then filter the user input based on this array.
Currently, I have implemented my custom adapter so ...
public class StationAdapter extends BaseAdapter implements Filterable {
Context _ctx;
ArrayList<Station> _stations;
private ArrayList<Station> orig;
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);
}
@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 StationView(_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 triggers every other input key. my main.xml looks just like that ...
<AutoCompleteTextView android:id="@+id/search_stations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:singleLine="true"
/>
Can anyone point out what I'm doing wrong, and some tutorials that relate to such a use case.
Thanks in advance
source
share