0

I'm building a chat app where users can mention each other by typing the '@' symbol followed by a username. I'm using an AutoCompleteTextView to show a list of suggested usernames as the user types. I'm populating the adapter for the AutoCompleteTextView by implementing a callback interface in my activity and using the resulting list of usernames to create an ArrayAdapter.

I want the AutoCompleteTextView to only show suggestions when the user is typing the '@' symbol, so I've added some code to check for the presence of the '@' character and set the dropdown height of the AutoCompleteTextView accordingly.

However, when I start typing a username and the filter is applied to the ArrayAdapter, I'm getting only a part of the full list of usernames, but multiple times. I suspect that the issue is in the performFiltering method of the ArrayAdapter, where I'm iterating over the full list of usernames and adding them to the suggestions list if they meet certain conditions.

Can anyone help me figure out why I'm getting duplicates in my AutoCompleteTextView suggestions and how I can fix this issue?

so this is my code

in my activity I implement the interface:

implements ChatViewModel.Callback

And getting the callback like this:

@Override
public void onComplete(List<String> result) {
    MentionAdapter mentionAdapter = new MentionAdapter(ChatActivity.this, result);
   binding.chatEtText.setAdapter(mentionAdapter);
}

And I'm calling it right away in the onCreate:

chatViewModel.getLiveMentionChannelsSearch();

But I only want that the AutoComplete should show up when a user is pressing the "@"character. So I did this:

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String mention = binding.chatEtText.getText().toString().toLowerCase();
            if (!binding.chatEtText.getText().toString().contains("@") ){
                 binding.chatEtText.setDropDownHeight(0);
            }else {
                binding.chatEtText.setDropDownHeight(500);
            }

And in the ArrayAdapter I am doing this:

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        List<String> suggestions = new ArrayList<>();
        String filterPattern = constraint.toString().toLowerCase().trim();
            for(String item:MentionItemsFull){
                if (filterPattern.length() >1) {
                    charAtZero =Character.toString(filterPattern.charAt(1));
                }
                if (filterPattern.startsWith("@")){
                    suggestions.addAll(MentionItemsFull);
                }
                if (charAtZero != null && item.toLowerCase().startsWith(charAtZero) ){
                    suggestions.add(item);
                }
                if (filterPattern.contains(" ")){
                    suggestions.clear();
                }
            }
        results.values = suggestions;
        results.count = suggestions.size();
        return results;
    }

The MentionItemsFull is the full list without any filter from the constructor.

private List<String> MentionItemsFull;
public MentionAdapter(@NonNull Context context, List<String> MentionItemList) {
    super(context, 0, MentionItemList);
    MentionItemsFull = new ArrayList<>(MentionItemList);
}

But the problem is that, when I am pressing the "@" character, I am getting only a part of the full list but multiple times.

Anyone know why?

0 Answers0