Android 開發(fā)即時聊天工具 YQ :(八) 添加/刪除好友
|
admin
2013年2月25日 14:28
本文熱度 3912
|
國際慣例,先上圖:
代碼比較簡單,只需給listview設置Listener就OK,
當點擊刪除后,這里沒有做提示,會直接刪除,刪除后應當給服務器發(fā)送一個刪除好友的包,服務器端更新數(shù)據(jù)庫中的數(shù)據(jù)。
然后客戶端要做的就是更新下好友列表。
具體看代碼吧:
- 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,"發(fā)起會話");
- 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);
- }
該文章在 2013/2/25 14:28:50 編輯過
|
|