gpt4 book ai didi

java - 隐藏数组适配器中的元素,而不是删除它们

转载 作者:行者123 更新时间:2023-11-30 01:23:27 24 4
gpt4 key购买 nike

我试图从 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;
}
}

最佳答案

getCount()getItem() 中执行:

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

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

只需确保在 getView() 中使用 getItem()

关于java - 隐藏数组适配器中的元素,而不是删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com