0

What i want to do

i want to mention both people and group in a chat activity.when the user types @ i want populate the list which contains no of user and channels so that he can mention both people and channels

What i have done

i took help from this question Android: Autocomplete TextView Similar To The Facebook App here i have included my code

1.Arraylist which has users

    ArrayList<User> userArrayList = new ArrayList<User>();
        for (User user : SocketSingleton.userMap.values()) {
            if (user.getId() != loggedUserId) {
                userArrayList.add(user);
            }
        }

2.Arraylist which has groups list

ArrayList<JoinedChannel> searchArrayList = new ArrayList<JoinedChannel>();
        for (JoinedChannel channel : SocketSingleton.listchannels.values()) {
            searchArrayList.add(channel);
        }

3.Adapters for view

final UserAdapter customAdapter = new UserAdapter(getActivity(), R.layout.all_user_list_item, userArrayList);

final ChannelAdapter customAdapter1 = new ChannelAdapter(getActivity(), R.layout.all_cahnnel_list_item, ChannelArrayList);

4.MultiAutocompletetextview

textinput = (MultiAutoCompleteTextView) view.findViewById(R.id.message_input);
        textinput.setAdapter(Adapter);
        textinput.setThreshold(0);

        textinput.setTokenizer(new MultiAutoCompleteTextView.Tokenizer() {

            @Override
            public CharSequence terminateToken(CharSequence text) {
                int i = text.length();

                while (i > 0 && text.charAt(i - 1) == ' ') {
                    i--;
                }

                if (i > 0 && text.charAt(i - 1) == ' ') {
                    return text;
                } else {
                    if (text instanceof Spanned) {
                        SpannableString sp = new SpannableString(text + " ");
                        TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
                        return sp;
                    } else {
                        return text + " ";
                    }
                }
            }

            @Override
            public int findTokenStart(CharSequence text, int cursor) {
                int i = cursor;

                while (i > 0 && text.charAt(i - 1) != '@') {
                    i--;
                }

                //Check if token really started with @, else we don't have a valid token
                if (i < 1 || text.charAt(i - 1) != '@') {
                    return cursor;
                }

                return i;
            }

            @Override
            public int findTokenEnd(CharSequence text, int cursor) {
                int i = cursor;
                int len = text.length();

                while (i < len) {
                    if (text.charAt(i) == ' ') {
                        return i;
                    } else {
                        i++;
                    }
                }

                return len;
            }
        });
        textinput.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Layout layout = textinput.getLayout();
                int pos = textinput.getSelectionStart();
                int line = layout.getLineForOffset(pos);
                int baseline = layout.getLineBaseline(line);

                int bottom = textinput.getHeight();

                textinput.setDropDownVerticalOffset(baseline - bottom);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        textinput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int id, KeyEvent event) {
                if (id == R.id.send || id == EditorInfo.IME_NULL) {
                    messageSend();
                    return true;
                }
                return false;
            }
        });

my adapters

Channel Adapter;

public class ChannelAdapter extends JoinChannelAdapter {
private LayoutInflater layoutInflater;
List<JoinedChannel> channelList;
Context context;
int resLayout;
private Filter mfilter = new Filter() {
    public String convertResultToString(Object resultValue) {
        return ((JoinedChannel) resultValue).getName();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults fr = new FilterResults();
        if (constraint != null) {
            ArrayList<JoinedChannel> suggestions = new ArrayList<JoinedChannel>();
            for (JoinedChannel list : channelList) {
                if (list.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    suggestions.add(list);
                }
            }
            fr.values = suggestions;
            fr.count = suggestions.size();
        }
        return fr;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults fr) {
        clear();
        if (fr != null && fr.count > 0) {
            addAll((ArrayList<JoinedChannel>) fr.values);
        }

        notifyDataSetChanged();
    }
};

public ChannelAdapter(Context context, int textViewresourceid, ArrayList<JoinedChannel> channel) {
    super(context, textViewresourceid, channel);
    channelList = new ArrayList<JoinedChannel>(channel.size());
    channelList.addAll(channel);
    layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    resLayout = textViewresourceid;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final JoinedChannel channel = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.all_cahnnel_list_item, parent, false);
    }

    TextView tvName = (TextView) convertView.findViewById(R.id.itemName);

    tvName.setText(channel.getName());


    convertView.setTag(channel);
    return convertView;
}

@Override
public Filter getFilter() {
    return mfilter;
}

UserAdapter

public class ChannelAdapter extends JoinChannelAdapter {
private LayoutInflater layoutInflater;
List<JoinedChannel> channelList;
Context context;
int resLayout;
private Filter mfilter = new Filter() {
    public String convertResultToString(Object resultValue) {
        return ((JoinedChannel) resultValue).getName();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults fr = new FilterResults();
        if (constraint != null) {
            ArrayList<JoinedChannel> suggestions = new ArrayList<JoinedChannel>();
            for (JoinedChannel list : channelList) {
                if (list.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    suggestions.add(list);
                }
            }
            fr.values = suggestions;
            fr.count = suggestions.size();
        }
        return fr;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults fr) {
        clear();
        if (fr != null && fr.count > 0) {
            addAll((ArrayList<JoinedChannel>) fr.values);
        }

        notifyDataSetChanged();
    }
};

public ChannelAdapter(Context context, int textViewresourceid, ArrayList<JoinedChannel> channel) {
    super(context, textViewresourceid, channel);
    channelList = new ArrayList<JoinedChannel>(channel.size());
    channelList.addAll(channel);
    layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    resLayout = textViewresourceid;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final JoinedChannel channel = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.all_cahnnel_list_item, parent, false);
    }

    TextView tvName = (TextView) convertView.findViewById(R.id.itemName);

    tvName.setText(channel.getName());


    convertView.setTag(channel);
    return convertView;
}

@Override
public Filter getFilter() {
    return mfilter;
}

}

Problem i have

with this code i can able to mention only people. i can only set either adapter1 or adapter2 for Autocomplete tectview. So it is only displaying either users or groups. i want to display all list in single adapter so that the user can mention peoples and groups i am new to this please help me to find a way

Community
  • 1
  • 1
Sabarinathan
  • 439
  • 1
  • 7
  • 19
  • Before set adapter to MultiAutoCompleteTextView can you add users and groups list into another list. And use that list for MultiAutoCompleteTextView – Pavya Nov 25 '15 at 12:53
  • i tried to do that. But after adding them into another list how can i set that list to my autocompletetextview – Sabarinathan Nov 25 '15 at 13:01
  • make one model/pogo class that can be used for users and groups arraylist. So you can create third arraylist by addin above two – Pavya Nov 25 '15 at 13:03
  • yeah i did that too followed this help http://stackoverflow.com/questions/33805996/how-to-add-two-arraylist-which-has-different-data-in-android-to-get-a-single-listview but i dont the coding for Third Adapter for new list view to set to MultiAutocompletetextview – Sabarinathan Nov 25 '15 at 13:06
  • Or can we set the third list view directly to MultiAutocompletetextview? – Sabarinathan Nov 25 '15 at 13:13

0 Answers0