Android 开发即时聊天工具 YQ :(八) 添加/删除好友
国际惯例,先上图:
代码比较简单,只需给listview设置Listener就OK,
当点击删除后,这里没有做提示,会直接删除,删除后应当给服务器发送一个删除好友的包,服务器端更新数据库中的数据。
然后客户端要做的就是更新下好友列表。
具体看代码吧:
private void setListViewListener() {listView.setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView<?> a, View v, int position,long l) {temp= (BuddyEntity) listView.getItemAtPosition(position);//打开聊天页面Intent intent=new Intent(BuddyActivity.this,ChatActivity.class);intent.putExtra("avatar", temp.getAvatar());intent.putExtra("account",temp.getAccount());intent.putExtra("nick", temp.getNick());startActivity(intent);} }); listView.setOnItemLongClickListener(new OnItemLongClickListener(){public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long arg3) {temp= (BuddyEntity) listView.getItemAtPosition(position);listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){public void onCreateContextMenu(ContextMenu menu,View arg1, ContextMenuInfo arg2) {menu.setHeaderTitle("操作");menu.add(0,0,0,"发起会话");menu.add(0,1,0,"删除好友");menu.add(0,2,0,"查看好友资料");}});return false;} });}public boolean onContextItemSelected(MenuItem item) {switch(item.getItemId()){case 0://打开聊天页面Intent intent=new Intent(BuddyActivity.this,ChatActivity.class);intent.putExtra("avatar", temp.getAvatar());intent.putExtra("account",temp.getAccount());intent.putExtra("nick", temp.getNick());startActivity(intent);break;case 1://向服务器发送一个删除好友的包SendMessage.sendADbuddy(MoreActivity.me.getAccount(), temp.getAccount(), YQMessageType.DEL_BUDDY);//删除好友列表中的该好友for(int i=0;i<buddyEntityList.size();i++){if((buddyEntityList.get(i).getAccount())==temp.getAccount()){buddyEntityList.remove(i);}}listView = (ListView) findViewById(R.id.listview);ba=new BuddyAdapter(this,buddyEntityList); listView.setAdapter(ba);break;case 2://break;}return super.onContextItemSelected(item);}
话说中午饭还没吃呢,吃饭去了。