androidtextviewmultiautocompletetextview

To split Strings from a MultiAutoCompleteTextView and set it to different textview


I have a array of strings which is a resultant of options selected from a MultiAutoCompleteTextView using the comma tokenizer. The string appears as below: "India, China, Japan, America, Australia"

I need to seperate this values based on the coma positiona and set those values to different textview's. Also I need to restrict the users to select only 5 values and those values should not be repeated.


Solution

  • You can use String.split() like this:

    String[] array = yourString.split(",");
    

    And iterate through that array.

    EDIT:

    To check if the user already selected the item, you can add an OnItemClickListener to your multiAutoCompleteTextView and have a HashSet or a Set where your items clicked are stored and check if this item already exists in the set

    Example:

    Initialize your HashSet first: HashSet<String> hashset = new HashSet<String>();

    And later:

    youMultiAutoCompleteTv.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                String itemClicked = parent.getAdapter().getItem(position).toString();
                if(hashset.contains(itemClicked)){
                    Toast.makeText(getApplicationContext(), "Item exists", Toast.LENGTH_SHORT).show();
                }
                else {
                    hashset.add(itemClicked);
                }
    
    
            }
        });