public void run() {

      while (isThreadReady) {
        try {
          sleep(1 * 1000);
          threadFriendList = localDatabase.getNewFriends();
          threadCommingCV = localDatabase.getNewCommingCV();
          if (threadFriendList != null) {
            Message message = new Message();
            for (int count = 0; count < threadFriendList.size(); count++) {
              ChatFriend friend = localDatabase.findFriendById(threadFriendList.get(count));
              if (friend.getFriendId() != utilSrvc.getSystemID()) {
                message.what = 100;
              } else {
                message.what = 300;
              }
              addNewFriend(friend);
            }
            threadHandler.sendMessage(message);
          }
          if (threadCommingCV) {
            Message message = new Message();
            message.what = 200;
            hasNewCV = threadCommingCV;
            threadHandler.sendMessage(message);
          }
        } catch (InterruptedException e) {
          // e.printStackTrace();
        }
      }
    }
 @Override
 public void onClick(View view) {
   ChatFriend systemFriend = new ChatFriend();
   systemFriend.setFriendId(utilSrvc.getSystemID());
   systemFriend.setName(getResources().getString(R.string.action_system_message));
   localDatabase.removeNewFriend(systemFriend.getFriendId());
   redIconForNewSystem.setVisibility(View.GONE);
   utilSrvc.gotoChatPage(context, systemFriend);
 }
 private void loadData() {
   List<ChatFriend> friends = localDatabase.findAllFriends();
   setDefaultSystemMessage();
   if (friendListView.getAdapter() != null) {
     friendList = new ArrayList<ChatFriend>();
     friendListView.setAdapter(listAdapter);
   }
   if (friends != null && !friends.isEmpty()) {
     for (int count = 0; count < friends.size(); count++) {
       ChatFriend friend = friends.get(count);
       if (friend.getFriendId() != utilSrvc.getSystemID()) {
         friendList.add(friend);
       }
     }
   }
 }
 public void addNewFriend(ChatFriend friend) {
   boolean canBeAdded = true;
   if (newMessageFriends != null) {
     for (int count = 0; count < newMessageFriends.size(); count++) {
       if (friend.getFriendId() == newMessageFriends.get(count).getFriendId()) {
         canBeAdded = false;
       }
     }
     if (canBeAdded) {
       newMessageFriends.add(friend);
     }
   } else {
     newMessageFriends = new ArrayList<ChatFriend>();
     newMessageFriends.add(friend);
   }
 }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ChatFriend friend = this.getItem(position);
      if (convertView == null) {
        LayoutInflater layoutInflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.activity_chat_friend_item, null);
      }
      ImageView redpoint = (ImageView) convertView.findViewById(R.id.chat_friend_new_message);
      TextView name = (TextView) convertView.findViewById(R.id.chat_friend_name);
      name.setText(friend.getName());

      TextView lastMessage = (TextView) convertView.findViewById(R.id.chat_list_last_message);
      TextView lastTime = (TextView) convertView.findViewById(R.id.chat_list_last_message_time);
      ChatMessage lastMessageText = localDatabase.findLastMessage(friend.getFriendId());
      Button delete = (Button) convertView.findViewById(R.id.chat_list_delete);
      redpoint.setVisibility(View.GONE);

      if (newMessageFriends.size() != 0) {
        for (int count = 0; count < newMessageFriends.size(); count++) {
          if (newMessageFriends.get(count) != null) {
            if (newMessageFriends.get(count).getName().equalsIgnoreCase(friend.getName())) {
              redpoint.setVisibility(View.VISIBLE);
            }
          }
        }
      }

      if (friend.getPortrait() != null) {
        ImageView portrait = (ImageView) convertView.findViewById(R.id.chat_friend_profile);
        Bitmap imageBitmap =
            BitmapFactory.decodeByteArray(friend.getPortrait(), 0, friend.getPortrait().length);
        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 50, 50, true);
        portrait.setImageBitmap(imageBitmap);
      }

      if (lastMessageText != null && lastMessageText.getDate() != null) {
        lastMessage.setText(lastMessageText.getText());
        lastTime.setText(lastMessageText.getDate().substring(5));
      }

      convertView.setOnTouchListener(new ItemTouchListener(delete, position, redpoint));
      delete.setOnClickListener(new DeleteListener(friend));

      return convertView;
    }