How to update ArrayList used to store KEY in MultiAutoCompleteTextView

209 Views Asked by At

I applied a MultiAutoCompleteTextView in my application. Most of the function may be treat as RECEIVER box of email app. I used a custom adapter with hashMap to store the Name and Id as sources of suggestion. When user click the name suggested (only name will show at suggestion list) the Id will programmatically add in a ID_list(arrayList) for next operation.

The problem is, user may click wrongly or change mind

1) How can I update the arrayList(ID_list) when user delete some of the names in MultiAutoCompleteTextView ??

2) How to delete a item (in multiautocompletetextview) with one Backspace click but not one character by on character??

Thank you very much.

1

There are 1 best solutions below

0
On BEST ANSWER

Well, I realize the importance of "simplization".

  1. Use a TextWatcher to update the arrayList(ID_list);

    inputReceiver.addTextChangedListener(new TextWatcher() { private int noOfCharAdded = 0; private int noOfCharDeleted = 0;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            noOfCharAdded = after;
            noOfCharDeleted = count;
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            KEY_MAC = null;
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
            if (noOfCharAdded >= 1 && noOfCharDeleted > 1) {
                // just input select and backspace
                System.out.println("just input select and backspace "
                        + receiverList.size());
                receiverList.remove(receiverList.size() - 1);
            }
    
        }
    });
    
  2. Use a onClickListener to set the cursor to end of string;

    inputReceiver.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            inputReceiver.setSelection(inputReceiver.getText().length());
        }
    });