android-edittexttextviewautocompletetextviewtokenautocompleteandroid-chipsandroid

Adding items to TokenAutoComplete Android


I am using the TokenAutoComplete library for adding Gmail style chips to my textfield. Everything is working fine. The only problem is that i want to add items to my ChipTextView when the UI is loaded but i cannot find any way to do that. All the items that i add to adapter are displayed as suggestion instead.

My ChipTextView class:

public class ChipTextView extends TokenCompleteTextView {

    public ChipTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ChipTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ChipTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected Object defaultObject(String text) {
        return text;
    }

    @Override
    protected View getViewForObject(Object text) {
        String hashtag = (String) text;
        LayoutInflater l = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) l.inflate(R.layout.chiptextview_item,
                (ViewGroup) ChipTextView.this.getParent(), false);
        ((TextView) view.findViewById(R.id.tv_text)).setText(hashtag);

        return view;
    }
}

My fragment From where i am setting up ChipTextView :

ArrayList<String> list=new ArrayList<>();
list.add("hello");
list.add("hi");
list.add("how");

ChipTextView  tv_chipview=(ChipTextView ) parentView.findViewById(R.id.tv_chipview);
 ArrayAdapter<String> mAdapter;
mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,list);
tv_chipview.setAdapter(mAdapter);
tv_chipview.allowDuplicates(false);
tv_chipview.setDeletionStyle(TokenDeleteStyle.Clear);

This is how it is shown when i add items to adapter using code Image after setting up my UI

How i want the output to be displayed after setting up. This is how it is displayed when i use my keyboard:

What it looks like if i add items mannually using keyboard

How can i add items to ChipTextView from code so that it seems like i had added them by using keyboard ?


Solution

  • I found the solution to my problem on TokenAutoComplete github documentation. I don't know how i missed this before but i have finally found it. :)

    For anyone who has faced the same problem i suggest you use addObject() method for adding items into ChipTextView.