我想要做的就是失败,这是:
在我看来,我有一个自动完成的Textview。 我想通过GET获取一个json对象数组。需要大约1-2秒...... (我应该使用AsyncTask还是Handler来支持此提取?)
然后根据此数组过滤用户的输入。
目前我已经实现了自定义适配器......
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"
/>
有人可以指出我做错了什么以及一些处理这种用例的教程
提前致谢
答案 0 :(得分:0)
这已经很晚了,但我相信这是预期的行为。过滤逻辑将对请求进行排队,而另一个请求是传出的。因此,如果您键入“a”,它将获取结果,之后的任何字母将等待“a”的结果返回。在这些结果返回后,新请求将会消失,依此类推。
答案 1 :(得分:0)
萨拉姆 我找到了解决方案 这是您必须制作的代码
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;
}