你的位置:首页 > Java教程

[Java教程]硅谷社交9


 

1.页面布局

<?

  

2.Listview初始化

// 创建适配器mInviteAdapter = new InviteAdapter(this, mOnInviteListener);// 添加适配器lv_invite.setAdapter(mInviteAdapter);

  

3.Listview适配器

 1 public class InviteAdapter extends BaseAdapter { 2   private Context mContext; 3   private List<InvationInfo> mInvitationInfos = new ArrayList<>(); 4   private OnInviteListener mOnInviteListener; 5   private InvationInfo invationInfo; 6  7   public InviteAdapter(Context context, OnInviteListener onInviteListener) { 8     mContext = context; 9  10     mOnInviteListener = onInviteListener; 11   } 12  13   // 刷新数据的方法 14   public void refresh(List<InvationInfo> invationInfos) { 15  16     if (invationInfos != null && invationInfos.size() >= 0) { 17  18       mInvitationInfos.clear(); 19  20       mInvitationInfos.addAll(invationInfos); 21  22       notifyDataSetChanged(); 23     } 24   } 25  26   @Override 27   public int getCount() { 28     return mInvitationInfos == null ? 0 : mInvitationInfos.size(); 29   } 30  31   @Override 32   public Object getItem(int position) { 33     return mInvitationInfos.get(position); 34   } 35  36   @Override 37   public long getItemId(int position) { 38     return position; 39   } 40  41   @Override 42   public View getView(int position, View convertView, ViewGroup parent) { 43     // 1 获取或创建viewHolder 44     ViewHodler hodler = null; 45     if (convertView == null) { 46       hodler = new ViewHodler(); 47  48       convertView = View.inflate(mContext, R.layout.item_invite, null); 49  50       hodler.name = (TextView) convertView.findViewById(R.id.tv_invite_name); 51       hodler.reason = (TextView) convertView.findViewById(R.id.tv_invite_reason); 52  53       hodler.accept = (Button) convertView.findViewById(R.id.bt_invite_accept); 54       hodler.reject = (Button) convertView.findViewById(R.id.bt_invite_reject); 55  56       convertView.setTag(hodler); 57     } else { 58       hodler = (ViewHodler) convertView.getTag(); 59     } 60  61     // 2 获取当前item数据 62     invationInfo = mInvitationInfos.get(position); 63  64     // 3 显示当前item数据 65     UserInfo user = invationInfo.getUser(); 66  67     if (user != null) {// 联系人 68       // 名称的展示 69       hodler.name.setText(invationInfo.getUser().getName()); 70  71       hodler.accept.setVisibility(View.GONE); 72       hodler.reject.setVisibility(View.GONE); 73  74       // 原因 75       if (invationInfo.getStatus() == InvationInfo.InvitationStatus.NEW_INVITE) {// 新的邀请 76  77         if (invationInfo.getReason() == null) { 78           hodler.reason.setText("添加好友"); 79         } else { 80           hodler.reason.setText(invationInfo.getReason()); 81         } 82  83         hodler.accept.setVisibility(View.VISIBLE); 84         hodler.reject.setVisibility(View.VISIBLE); 85  86       } else if (invationInfo.getStatus() == InvationInfo.InvitationStatus.INVITE_ACCEPT) {// 接受邀请 87  88         if (invationInfo.getReason() == null) { 89           hodler.reason.setText("接受邀请"); 90         } else { 91           hodler.reason.setText(invationInfo.getReason()); 92         } 93       } else if (invationInfo.getStatus() == InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER) {// 邀请被接受 94         if (invationInfo.getReason() == null) { 95           hodler.reason.setText("邀请被接受"); 96         } else { 97           hodler.reason.setText(invationInfo.getReason()); 98         } 99       }100 101       // 按钮的处理102       hodler.accept.setOnClickListener(new View.OnClickListener() {103         @Override104         public void onClick(View v) {105           mOnInviteListener.onAccept(invationInfo);106         }107       });108 109       // 拒绝按钮的点击事件处理110       hodler.reject.setOnClickListener(new View.OnClickListener() {111         @Override112         public void onClick(View v) {113           mOnInviteListener.onReject(invationInfo);114         }115       });116     } else {// 群组117       // 显示名称118       hodler.name.setText(invationInfo.getGroup().getInvatePerson());119 120       hodler.accept.setVisibility(View.GONE);121       hodler.reject.setVisibility(View.GONE);122 123       // 显示原因124       switch(invationInfo.getStatus()){125         // 您的群申请请已经被接受126         case GROUP_APPLICATION_ACCEPTED:127           hodler.reason.setText("您的群申请请已经被接受");128           break;129         // 您的群邀请已经被接收130         case GROUP_INVITE_ACCEPTED:131           hodler.reason.setText("您的群邀请已经被接收");132           break;133 134         // 你的群申请已经被拒绝135         case GROUP_APPLICATION_DECLINED:136           hodler.reason.setText("你的群申请已经被拒绝");137           break;138 139         // 您的群邀请已经被拒绝140         case GROUP_INVITE_DECLINED:141           hodler.reason.setText("您的群邀请已经被拒绝");142           break;143 144         // 您收到了群邀请145         case NEW_GROUP_INVITE:146           hodler.accept.setVisibility(View.VISIBLE);147           hodler.reject.setVisibility(View.VISIBLE);148 149           // 接受邀请150           hodler.accept.setOnClickListener(new View.OnClickListener() {151             @Override152             public void onClick(View v) {153               mOnInviteListener.onInviteAccept(invationInfo);154             }155           });156 157           // 拒绝邀请158           hodler.reject.setOnClickListener(new View.OnClickListener() {159             @Override160             public void onClick(View v) {161               mOnInviteListener.onInviteReject(invationInfo);162             }163           });164 165           hodler.reason.setText("您收到了群邀请");166           break;167 168         // 您收到了群申请169         case NEW_GROUP_APPLICATION:170           hodler.accept.setVisibility(View.VISIBLE);171           hodler.reject.setVisibility(View.VISIBLE);172 173           // 接受申请174           hodler.accept.setOnClickListener(new View.OnClickListener() {175             @Override176             public void onClick(View v) {177               mOnInviteListener.onApplicationAccept(invationInfo);178             }179           });180 181           // 拒绝申请182           hodler.reject.setOnClickListener(new View.OnClickListener() {183             @Override184             public void onClick(View v) {185               mOnInviteListener.onApplicationReject(invationInfo);186             }187           });188 189           hodler.reason.setText("您收到了群申请");190           break;191 192         // 你接受了群邀请193         case GROUP_ACCEPT_INVITE:194           hodler.reason.setText("你接受了群邀请");195           break;196 197         // 您批准了群申请198         case GROUP_ACCEPT_APPLICATION:199           hodler.reason.setText("您批准了群申请");200           break;201 202         // 您拒绝了群邀请203         case GROUP_REJECT_INVITE:204           hodler.reason.setText("您拒绝了群邀请");205           break;206 207         // 您拒绝了群申请208         case GROUP_REJECT_APPLICATION:209           hodler.reason.setText("您拒绝了群申请");210           break;211       }212     }213 214     // 4 返回view215     return convertView;216   }217 218   private class ViewHodler {219     private TextView name;220     private TextView reason;221 222     private Button accept;223     private Button reject;224   }225 226 227   public interface OnInviteListener {228     // 联系人接受按钮的点击事件229     void onAccept(InvationInfo invationInfo);230 231     // 联系人拒绝按钮的点击事件232     void onReject(InvationInfo invationInfo);233 234     // 接受邀请按钮处理235     void onInviteAccept(InvationInfo invationInfo);236     // 拒绝邀请按钮处理237     void onInviteReject(InvationInfo invationInfo);238 239     // 接受申请按钮处理240     void onApplicationAccept(InvationInfo invationInfo);241     // 拒绝申请按钮处理242     void onApplicationReject(InvationInfo invationInfo);243   }244 }

InviteAdapter


4.Listview回调监听的接口

 1 private InviteAdapter.OnInviteListener mOnInviteListener = new InviteAdapter.OnInviteListener() { 2   @Override 3   public void onAccept(final InvationInfo invationInfo) { 4     // 通知环信服务器,点击了接受按钮 5     Model.getInstance().getGlobalThreadPool().execute(new Runnable() { 6       @Override 7       public void run() { 8         try { 9           EMClient.getInstance().contactManager().acceptInvitation(invationInfo.getUser().getHxid()); 10  11           // 数据库更新 12           Model.getInstance().getDbManager().getInviteTableDao().updateInvitationStatus(InvationInfo.InvitationStatus.INVITE_ACCEPT,invationInfo.getUser().getHxid()); 13  14           runOnUiThread(new Runnable() { 15             @Override 16             public void run() { 17               // 页面发生变化 18               Toast.makeText(InviteActivity.this, "接受了邀请", Toast.LENGTH_SHORT).show(); 19  20               // 刷新页面 21               refresh(); 22             } 23           }); 24  25         } catch (HyphenateException e) { 26           e.printStackTrace(); 27  28           runOnUiThread(new Runnable() { 29             @Override 30             public void run() { 31               Toast.makeText(InviteActivity.this, "接受邀请失败", Toast.LENGTH_SHORT).show(); 32             } 33           }); 34         } 35       } 36     }); 37   } 38  39   @Override 40   public void onReject(final InvationInfo invationInfo) { 41     Model.getInstance().getGlobalThreadPool().execute(new Runnable() { 42       @Override 43       public void run() { 44         try { 45           EMClient.getInstance().contactManager().declineInvitation(invationInfo.getUser().getHxid()); 46  47           // 数据库变化 48           Model.getInstance().getDbManager().getInviteTableDao().removeInvitation(invationInfo.getUser().getHxid()); 49  50           // 页面变化 51           runOnUiThread(new Runnable() { 52             @Override 53             public void run() { 54               Toast.makeText(InviteActivity.this, "拒绝成功了", Toast.LENGTH_SHORT).show(); 55  56               // 刷新页面 57               refresh(); 58             } 59           }); 60         } catch (HyphenateException e) { 61           e.printStackTrace(); 62  63           runOnUiThread(new Runnable() { 64             @Override 65             public void run() { 66               Toast.makeText(InviteActivity.this, "拒绝失败了", Toast.LENGTH_SHORT).show(); 67             } 68           }); 69         } 70       } 71     }); 72   } 73  74   // 接受邀请按钮 75   @Override 76   public void onInviteAccept(final InvationInfo invationInfo) { 77     Model.getInstance().getGlobalThreadPool().execute(new Runnable() { 78       @Override 79       public void run() { 80         try { 81           // 告诉环信服务器接受了邀请 82           EMClient.getInstance().groupManager().acceptInvitation(invationInfo.getGroup().getGroupId(), invationInfo.getGroup().getInvatePerson()); 83  84           // 本地数据更新 85           invationInfo.setStatus(InvationInfo.InvitationStatus.GROUP_ACCEPT_INVITE); 86           Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo); 87  88           // 内存数据的变化 89           runOnUiThread(new Runnable() { 90             @Override 91             public void run() { 92               Toast.makeText(InviteActivity.this, "接受邀请", Toast.LENGTH_SHORT).show(); 93  94               // 刷新页面 95               refresh(); 96             } 97           }); 98         } catch (HyphenateException e) { 99           e.printStackTrace();100           runOnUiThread(new Runnable() {101             @Override102             public void run() {103               Toast.makeText(InviteActivity.this, "接受邀请失败", Toast.LENGTH_SHORT).show();104             }105           });106         }107       }108     });109   }110 111   // 拒绝邀请按钮112   @Override113   public void onInviteReject(final InvationInfo invationInfo) {114     Model.getInstance().getGlobalThreadPool().execute(new Runnable() {115       @Override116       public void run() {117         try {118           // 告诉环信服务器拒绝了邀请119           EMClient.getInstance().groupManager().declineInvitation(invationInfo.getGroup().getGroupId(), invationInfo.getGroup().getInvatePerson(),"拒绝邀请");120 121           // 更新本地数据库122           invationInfo.setStatus(InvationInfo.InvitationStatus.GROUP_REJECT_INVITE);123           Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);124 125           // 更新内存的数据126           runOnUiThread(new Runnable() {127             @Override128             public void run() {129               Toast.makeText(InviteActivity.this, "拒绝邀请", Toast.LENGTH_SHORT).show();130 131               // 刷新页面132               refresh();133             }134           });135         } catch (HyphenateException e) {136           e.printStackTrace();137         }138       }139     });140   }141 142   // 接受申请按钮143   @Override144   public void onApplicationAccept(final InvationInfo invationInfo) {145     Model.getInstance().getGlobalThreadPool().execute(new Runnable() {146       @Override147       public void run() {148         try {149           // 告诉环信服务器接受了申请150           EMClient.getInstance().groupManager().acceptApplication(invationInfo.getGroup().getGroupId(), invationInfo.getGroup().getInvatePerson());151 152           // 更新数据库153           invationInfo.setStatus(InvationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION);154           Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);155 156           // 更新内存157           runOnUiThread(new Runnable() {158             @Override159             public void run() {160               Toast.makeText(InviteActivity.this, "接受申请", Toast.LENGTH_SHORT).show();161 162               refresh();163             }164           });165         } catch (HyphenateException e) {166           e.printStackTrace();167         }168       }169     });170   }171 172   // 拒绝申请按钮173   @Override174   public void onApplicationReject(final InvationInfo invationInfo) {175     Model.getInstance().getGlobalThreadPool().execute(new Runnable() {176       @Override177       public void run() {178         try {179           // 告诉环信服务器拒绝了申请180           EMClient.getInstance().groupManager().declineApplication(invationInfo.getGroup().getGroupId(),invationInfo.getGroup().getInvatePerson(),"拒绝申请");181 182           // 更新本地数据库183           invationInfo.setStatus(InvationInfo.InvitationStatus.GROUP_REJECT_APPLICATION);184           Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);185 186           // 更新内存187           runOnUiThread(new Runnable() {188             @Override189             public void run() {190               Toast.makeText(InviteActivity.this, "拒绝申请", Toast.LENGTH_SHORT).show();191 192               refresh();193             }194           });195         } catch (HyphenateException e) {196           e.printStackTrace();197 198           runOnUiThread(new Runnable() {199             @Override200             public void run() {201               Toast.makeText(InviteActivity.this, "拒绝申请失败", Toast.LENGTH_SHORT).show();202             }203           });204         }205       }206     });207   }208 };

OnInviteListener

 

5.注册联系人邀请信息变化的广播

mLBM = LocalBroadcastManager.getInstance(this);mLBM.registerReceiver(InviteChangedReceiver, new IntentFilter(Constant.CONTACT_INVITE_CHANGED));private BroadcastReceiver InviteChangedReceiver = new BroadcastReceiver() {    @Override    public void onReceive(Context context, Intent intent) {      ///联系人邀请信息变化       mInviteAdapter.refresh(Model.getInstace().getDbManager().getInviteTableDao().getInvitations());    }};