实战 QQ demo源码(本例中有该应用)
服务器端下载:http://download.csdn.net/download/knight_black_bob/9822551
android eclipse 版:http://download.csdn.net/download/knight_black_bob/9822553
android stdio 版本:http://download.csdn.net/download/knight_black_bob/9822556
运行结果:
日志打印:
流程图:
以上为eventbus 使用,下面可以作为正常逻辑
1
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mBaseView = LayoutInflater.from(getActivity()).inflate(R.layout.friendship_list_fragment, null); initUtils(); initView(mBaseView); initListeners(); EventBus.getDefault().register( this ); FriendshipUserRequest.getFriendshipUserFromWeb(user.getId()); return mBaseView; }
2
public class FriendshipUserRequest { public static void getFriendshipUserFromWeb(long userId ){ GsonRequest<QueryResultJson> gsonRequest = new GsonRequest<QueryResultJson>( Constants.friendship_url+ userId, QueryResultJson.class, new Response.Listener<QueryResultJson>() { @Override public void onResponse(QueryResultJson response) { JsonElement obj = response.retdata; if (obj != null && response.retcode == 200) { Log.e("baoyou","obj.toString() == "+ obj.toString()); FriendshipUserEntity resultjson = JsonParser.parseDateJson(obj.toString(), FriendshipUserEntity.class); FriendshipUserHttpManager.getFriendshipUserFromWeb(resultjson); }else{ } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) ; Volley.newRequestQueue(MyApplication.newInstance()).add(gsonRequest); } }
3
public class FriendshipUserHttpManager { public static void getFriendshipUserFromWeb( FriendshipUserEntity item){ List<FriendshipUserEntity> list = new ArrayList<FriendshipUserEntity>(); list.add(item); FriendshipUserHttpEvent dbEvent = new FriendshipUserHttpEvent(); dbEvent.status = list.size() == 0? RequestStatus.HTTP_NONE: RequestStatus.HTTP_SUCCESS; dbEvent.mDataList = list; Log.e("baoyou", "--"+list.size() + list.toString()); EventBus.getDefault().post(dbEvent); } }
4.
public void onEventMainThread(RequestEvent dbEvent) { Log.e("baoyou", "-- event ==================" ); if (dbEvent instanceof FriendshipUserHttpEvent) { Log.e("baoyou", "-- event http ==================" ); FriendshipUserHttpEvent event = (FriendshipUserHttpEvent) dbEvent; switch (dbEvent.status) { case HTTP_NONE: break; case HTTP_SUCCESS: { FriendshipUserEntity item = event.mDataList.get(0); userDB.deleteAll(); friendShipDB.deleteAll(); for (UserEntity ue : item.userList) { userDB.saveOrUpdate(ue); } for (FriendShipEntity fse : item.friendShipList) { friendShipDB.saveOrUpdate(fse); } FriendShipDBManager.getAllFromDB(friendShipDB); } break; default: break; } } @Override public void onDestroy() { EventBus.getDefault().unregister( this ); super.onDestroy(); }
5.
public class FriendShipDBManager { public static void getAllFromDB(FriendShipDB friendShipDB){ List<FriendShipEntity> list = friendShipDB.getAll(); FriendShipDBEvent dbEvent = new FriendShipDBEvent(); dbEvent.status = list.size() == 0? RequestStatus.DB_NONE: RequestStatus.DB_SUCCESS; dbEvent.mDataList = list; EventBus.getDefault().post(dbEvent); } }
6.
else if (dbEvent instanceof FriendShipDBEvent) { Log.e("baoyou", "-- event db ==================" ); FriendShipDBEvent event = (FriendShipDBEvent) dbEvent; switch (dbEvent.status) { case DB_NONE: break; case DB_SUCCESS: { List<FriendShipEntity> list = event.mDataList; List<FriendShips> dataList = new ArrayList<FriendShips>(); for (FriendShipEntity fs : list) { UserEntity userEntityByIndex = null; if (fs.getUserId() != 0) userEntityByIndex = userDB.getEntityById(fs.getUserId() + ""); FriendShips item = new FriendShips(); item.setId(fs.getId()); item.setUserSrc(fs.getUserSrc()); item.setParentId(fs.getParentId()); item.setName(fs.getName()); if (userEntityByIndex != null) item.setName(userEntityByIndex.getNickName()); item.setUserId(fs.getUserId()); if (userEntityByIndex != null) item.setPhoto(userEntityByIndex.getPhoto()); dataList.add(item); } for (FriendShips fs : dataList) { if (fs.getParentId() == 0) { long id = fs.getId(); List<FriendShips> childs = new ArrayList<FriendShips>(); for (FriendShips fs2 : dataList) { if (id == fs2.getParentId()) { childs.add(fs2); } } fs.setChilds(childs); mDataList.add(fs); } } mAdapter.notifyDataSetChanged(); }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
android EventBus
AndroidEventBus 事件总线的jar,版本是1.0.2
android eventbus总线jar包就2积分,多下载,谢谢亲们。
Android EventBus框架使用详细介绍(一)
这个是一个简单使用EventBus的Demo,里面实现了在线程里通过EventBus发送消息给主线程,实现加载框的变化功能
android Event bus 3.0 加入注解的使用的方式
我们通常使用观察者模式,正式因为观察者模式非常常见,所以在jdk1.5中已经帮助我们实现了观察者模式,我们只需要简单的继承一些类就可以快速使用观察者模式,在Android中也有一个类似功能的开源库EventBus,...
Android EventBus使用,粘性事件postSticky使用 https://blog.csdn.net/qq_31939617/article/details/80860615
Android SystemUI源码中的EventBus实现,EventBus类文件。
AndroidEventBus是一个Android平台轻量级的事件总线框架, 它简化了Activity、Fragment、Service等组件之间的交互,很大程度上降低了它们之间的耦合,使得我们的代码更加简洁,耦合性更低,提升我们的代码质量。
AndroidEventBus框架的使用demo
android eventbus-3.2.0.jar 解压就可以使用
android eventbus 简单使用的详细介绍。
Android EventBus实例代码
EventBus是基于Otto的消息发送机制,经过开源大神们的封装,已经越来越好用了。
以新浪微博为例,在新浪微博首页好友动态列表页和好友动态...Android事件总线框架 AndroidEventBus 为此类需求的实现提供了非常方便的方案。 文章地址:http://blog.csdn.net/kpioneer123/article/details/51260660
android EventBus源码.zip
代码为博客实例代码:http://blog.csdn.net/lmj623565791/article/details/40794879 有问题博客留言
Android Eventbus
主要是通信组件EventBus,EventBus是一款针对Android优化的发布/订阅事件总线。 博客地址:http://blog.csdn.net/dickyqie/article/details/55101035