@Mention使用Multiautocompletetextview和ArrayAdapter <object1>,ArrayAdapter <object2>而不是一个适配器

时间:2015-11-25 12:43:08

标签: android arraylist adapter autocompletetextview

我想做什么

我想在聊天活动中提及人和群组。当用户输入@我想要填充不包含用户和频道的列表 所以他可以提到人和渠道

我做了什么

我从这个问题中获得了帮助Android: Autocomplete TextView Similar To The Facebook App 这里我已经包含了我的代码

1.Arraylist拥有用户

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

2.Arraylist有组列表

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

3.视图适配器

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;
            }
        });

我的适配器

频道适配器;

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;
}

}

我有问题

使用此代码我只能提及人。我只能为自动完成tectview设置adapter1或adapter2。所以它只显示用户或组。 我想在单个适配器中显示所有列表,以便用户可以提及人员和组 我是新手,请帮助我找到方法

0 个答案:

没有答案