Esempio n. 1
0
    @Override
    protected synchronized FilterResults performFiltering(CharSequence prefix) {
      FilterResults results = new FilterResults();
      if (mOriginalList == null) {
        mOriginalList = new ArrayList<User>();
      }
      EMLog.d(TAG, "contacts original size: " + mOriginalList.size());
      EMLog.d(TAG, "contacts copy size: " + copyUserList.size());

      if (prefix == null || prefix.length() == 0) {
        results.values = copyUserList;
        results.count = copyUserList.size();
      } else {
        String prefixString = prefix.toString();
        final int count = mOriginalList.size();
        final ArrayList<User> newValues = new ArrayList<User>();
        for (int i = 0; i < count; i++) {
          final User user = mOriginalList.get(i);
          String username = user.getUsername();

          if (username.startsWith(prefixString)) {
            newValues.add(user);
          } else {
            final String[] words = username.split(" ");
            final int wordCount = words.length;

            // Start at index 0, in case valueText starts with space(s)
            for (int k = 0; k < wordCount; k++) {
              if (words[k].startsWith(prefixString)) {
                newValues.add(user);
                break;
              }
            }
          }
        }
        results.values = newValues;
        results.count = newValues.size();
      }
      EMLog.d(TAG, "contacts filter results size: " + results.count);
      return results;
    }
Esempio n. 2
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = layoutInflater.inflate(res, null);
      holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
      holder.unreadMsgView = (TextView) convertView.findViewById(R.id.unread_msg_number);
      holder.nameTextview = (TextView) convertView.findViewById(R.id.name);
      holder.tvHeader = (TextView) convertView.findViewById(R.id.header);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    User user = getItem(position);
    if (user == null) Log.d("ContactAdapter", position + "");
    // 设置nick,demo里不涉及到完整user,用username代替nick显示
    String username = user.getUsername();
    String header = user.getHeader();
    if (position == 0 || header != null && !header.equals(getItem(position - 1).getHeader())) {
      if (TextUtils.isEmpty(header)) {
        holder.tvHeader.setVisibility(View.GONE);
      } else {
        holder.tvHeader.setVisibility(View.VISIBLE);
        holder.tvHeader.setText(header);
      }
    } else {
      holder.tvHeader.setVisibility(View.GONE);
    }
    // 显示申请与通知item
    if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
      holder.nameTextview.setText(user.getNick());
      holder.avatar.setImageResource(R.drawable.new_friends_icon);
      if (user.getUnreadMsgCount() > 0) {
        holder.unreadMsgView.setVisibility(View.VISIBLE);
        //			    holder.unreadMsgView.setText(user.getUnreadMsgCount()+"");
      } else {
        holder.unreadMsgView.setVisibility(View.INVISIBLE);
      }
    } else if (username.equals(Constant.GROUP_USERNAME)) {
      // 群聊item
      holder.nameTextview.setText(user.getNick());
      holder.avatar.setImageResource(R.drawable.groups_icon);
    } else if (username.equals(Constant.CHAT_ROOM)) {
      // 群聊item
      holder.nameTextview.setText(user.getNick());
      holder.avatar.setImageResource(R.drawable.groups_icon);
    } else if (username.equals(Constant.CHAT_ROBOT)) {
      // Robot item
      holder.nameTextview.setText(user.getNick());
      holder.avatar.setImageResource(R.drawable.groups_icon);
    } else {
      holder.nameTextview.setText(username);
      // 设置用户头像
      UserUtils.setUserAvatar(getContext(), username, holder.avatar);
      if (holder.unreadMsgView != null) holder.unreadMsgView.setVisibility(View.INVISIBLE);
    }

    return convertView;
  }