Android的正确方法,如果实施过滤自动完成 [英] Android Correct way if implementing filterable in Auto-Complete

查看:221
本文介绍了Android的正确方法,如果实施过滤自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是和惨遭失败是这样的:

What i am trying to do is and failing miserably is this:

在我看来,我有一个自动完成的TextView。
我想通过一个GET对象的JSON数组来获取。需要约1-2秒...
(我应该使用AsyncTask的或处理程序来支持这种获取?)

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 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中看起来简直像这样...

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

在此先感谢

推荐答案

这是很晚了,但我相信这是预期的行为。而另一个要求是外向过滤逻辑将队列中的请求。所以,如果你输入一个a,它会取后的结果和任何字母(S)将等待,直到结果一回来。经过这些结果回来,一个新的请求将走出去,等等。

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.

这篇关于Android的正确方法,如果实施过滤自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆