我有一个Json arraylist。这里已经使用此自动填充文本视图设置了适配器文件。
从我的代码:
我使用了像USER这样的包装器类。在这个课程中,有3个字段(userid, username and designation)
。
这里我需要写一些文字意味着需要显示来自用户名和名称的建议。如果输入的单词与这两个字段中的任何一个匹配,建议需要显示。
我如何处理AutoCompleteTextView
。
ArrayAdapter<User> userAdapter = new ArrayAdapter<>(getActivity(), R.layout
.spinner_item_text,
UserList);
现在数组中显示的建议。但我需要像字符串一样显示建议。
对于EG:
UserList具有类似
的值{
"profile_view": [
{
username :"test",designation:"SE"
},
{
username :"test1",designation:"TE"
}]}
现在正在框中输入关键字,例如&#34; t&#34;意味着需要提出像
这样的建议 test
test1
TE
我该怎么办?请给我任何实现这个想法?
答案 0 :(得分:1)
编写自定义适配器并在该适配器中使用自定义筛选。
在下面的代码中,SearchSuggestion
是我的模型。您可以在performFiltering()
方法中更改模型并修改搜索条件。希望这有帮助!
public class SearchSuggestionAdapter extends ArrayAdapter<SearchSuggestion> implements Filterable {
private Context mContext;
private List<SearchSuggestion> mSuggestionList;
private List<SearchSuggestion> mFilteredSuggestionList;
public SearchSuggestionAdapter(Context context, List<SearchSuggestion> suggestionList) {
super(context, -1);
mContext = context;
mSuggestionList = suggestionList;
mFilteredSuggestionList = mSuggestionList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.suggestion_item, null);
viewHolder = new ViewHolder();
viewHolder.mSuggestionTextView = (TextView) convertView.findViewById(R.id.suggestion_text);
viewHolder.mSuggestionImage = (ImageView) convertView.findViewById(R.id.suggestion_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
SearchSuggestion searchSuggestion = mFilteredSuggestionList.get(position);
viewHolder.mSuggestionTextView.setText(searchSuggestion.getName());
viewHolder.mSuggestionImage.setImageResource(searchSuggestion.isTag() ? R.drawable.suggestion_tag_icon : R.drawable.suggestion_project_icon);
return convertView;
}
public static class ViewHolder {
private ImageView mSuggestionImage;
private TextView mSuggestionTextView;
}
@Override
public int getCount() {
return mFilteredSuggestionList.size();
}
@Override
public SearchSuggestion getItem(int position) {
return mFilteredSuggestionList.get(position);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
constraint = constraint.toString().toLowerCase();
List<SearchSuggestion> tempFilteredList = new ArrayList<>();
for (SearchSuggestion suggestion : mSuggestionList) {
String name = suggestion.getName();
if (name != null && name.toLowerCase().contains(constraint)) {
tempFilteredList.add(suggestion);
}
}
results.values = tempFilteredList;
results.count = tempFilteredList.size();
} else {
results.values = mSuggestionList;
results.count = mSuggestionList.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
mFilteredSuggestionList = (ArrayList<SearchSuggestion>) results.values;
notifyDataSetChanged();
}
}
};
}
}
答案 1 :(得分:0)
如果您希望在下拉列表中将您的用户名和名称作为建议,请将所有用户名和名称添加到一个阵列中,然后将其传递给适配器。