隐藏Array Adapter中的元素,而不是删除它们

时间:2016-04-20 20:08:46

标签: java android android-arrayadapter google-places-api

我试图在google places api中隐藏AutoCompleteTextView的数组适配器中的一些元素。我想要隐藏他们position%2==0的元素。我在堆栈溢出中找到了一些答案,建议在getView中执行此操作:

@Override
public View getView(int position, View view, ViewGroup parent) {
    if(position%2==0) {
        //something to do here
    }
    return view;
}

但我不知道我在评论区域要做什么。

以下是整个代码:

class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {
    private ArrayList resultList;

    public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public Object getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if(position%2==0) {
            //view.setVisibility() maybe?
        }
        return view;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    resultList = autocomplete(constraint.toString());
                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }
}

1 个答案:

答案 0 :(得分:1)

getCount()getItem()

中执行此操作
@Override
public int getCount() {
    return resultList.size() / 2;
}

@Override
public Object getItem(int index) {
    return resultList.get(index * 2 + 1);
}

请确保在getItem()中使用getView()