0

I have a Json arraylist. Here already set the adapter file with this Autocomplete text view.

From my code:

I have used a wrapper class like USER. In this class 3 fields are there (userid, username and designation).

here i need to write some text means need to show the suggestions from username and designation.if the typing word matches from any of these two fields suggestions need to display.

How can i do with the AutoCompleteTextView.

ArrayAdapter<User> userAdapter = new ArrayAdapter<>(getActivity(), R.layout
                                .spinner_item_text,
                                UserList);

Now the suggestions showing in the array. But i need to show the suggestion like string.

For EG :

UserList having the values like

{
  "profile_view": [
    {
username :"test",designation:"SE"
    },
{
username :"test1",designation:"TE"
    }]}

now am typing the keyword in the box like "t" means need to give the suggestions like

 test
 test1
 TE

How can i do ? Please give me any ideas to implement this?

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
Krishna Veni
  • 2,217
  • 8
  • 27
  • 53

2 Answers2

1

Write a custom Adapter and use custom filtering in that adapter. In the below code, SearchSuggestion is my model. You can change the model and modify the search condition in performFiltering() method. Hope this helps!

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();
        }
    }
};
}
}
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
0

If you want your User Name and the designations as a suggestion in the drop down, then add all the usernames and the designations in one array and pass it to the adapter.

Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21