程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 硅谷社交9--邀請信息列表頁面,9--信息列表

硅谷社交9--邀請信息列表頁面,9--信息列表

編輯:JAVA綜合教程

硅谷社交9--邀請信息列表頁面,9--信息列表


 

1.頁面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.hyphenate.easeui.widget.EaseTitleBar
        app:titleBarTitle="邀請信息列表頁面"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.hyphenate.easeui.widget.EaseTitleBar>
    
    <ListView
        android:id="@+id/lv_invite"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
</LinearLayout>

  

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                 @Override
104                 public void onClick(View v) {
105                     mOnInviteListener.onAccept(invationInfo);
106                 }
107             });
108 
109             // 拒絕按鈕的點擊事件處理
110             hodler.reject.setOnClickListener(new View.OnClickListener() {
111                 @Override
112                 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                         @Override
152                         public void onClick(View v) {
153                             mOnInviteListener.onInviteAccept(invationInfo);
154                         }
155                     });
156 
157                     // 拒絕邀請
158                     hodler.reject.setOnClickListener(new View.OnClickListener() {
159                         @Override
160                         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                         @Override
176                         public void onClick(View v) {
177                             mOnInviteListener.onApplicationAccept(invationInfo);
178                         }
179                     });
180 
181                     // 拒絕申請
182                     hodler.reject.setOnClickListener(new View.OnClickListener() {
183                         @Override
184                         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 返回view
215         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                         @Override
102                         public void run() {
103                             Toast.makeText(InviteActivity.this, "接受邀請失敗", Toast.LENGTH_SHORT).show();
104                         }
105                     });
106                 }
107             }
108         });
109     }
110 
111     // 拒絕邀請按鈕
112     @Override
113     public void onInviteReject(final InvationInfo invationInfo) {
114         Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
115             @Override
116             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                         @Override
128                         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     @Override
144     public void onApplicationAccept(final InvationInfo invationInfo) {
145         Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
146             @Override
147             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                         @Override
159                         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     @Override
174     public void onApplicationReject(final InvationInfo invationInfo) {
175         Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
176             @Override
177             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                         @Override
189                         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                         @Override
200                         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());
        }
};

  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved