Android The correct way that implements filtering in Auto-Complete

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;
//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();
    }
}

}

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

+3
source share
2 answers

, , . , - . , "a", () , "a" . , , ..

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;
            }
0

Source: https://habr.com/ru/post/1795688/


All Articles