什么,我试图做的是和失败草草收场是这样的:Android的正确的方式,如果在实施过滤自动完成
在我看来,我有一个自动完成的TextView。 我想通过GET获取一个json数组对象。全程约1-2秒... (我应该使用的AsyncTask或处理程序来支持这种获取?)
基于此数组然后过滤用户的输入。
目前,我已经实现了我的自定义适配器等等...
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 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();
}
}
}
出于某种原因,过滤响应每触发其它输入键我main.xml中看起来简直像这样.. 。
<AutoCompleteTextView android:id="@+id/search_stations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:singleLine="true"
/>
有人能指出我在做什么错误的,一些教程,这样的用例处理
在此先感谢