public voID getAttendants() { ArrayList<AttendModel> attendees = new ArrayList<AttendModel>(); FirebaseConnection.getInstance().getAllAttendeesFor(uuID, attendees); Log.d("attendees", String.valueOf(attendees.size()));}
@H_502_0@这是我的Firebase *** 作:@H_502_0@ public voID getAllAttendeesFor(String UUID, final ArrayList<AttendModel> attendarray) { final DatabaseReference attendobjects = getDatabasetableWith(Constants.tableAttendobject); query queryRef = attendobjects.orderByChild(Constants.taAttendobjectUUID).equalTo(UUID); queryRef.addListenerForSingleValueEvent(new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) { HashMap<String, Object> attend = (HashMap<String, Object>) postsnapshot.getValue(); String UUID = (String) attend.get(Constants.taAttendobjectUUID); String userUsername = (String) attend.get(Constants.AttendobjectUserUsername); AttendModel attendModel = new AttendModel(userUsername, UUID); attendarray.add(attendModel); //here would like to notify or somehow return the attendarray } } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } });}
@H_502_0@AndroID中最好的方法是什么?是否有可能实现各种适配器或完成处理程序?解决方法:
@H_502_0@您正在寻找的是Callback接口,根据您的需要,您需要使用单个参数实现单个方法的简单接口,因为Java 6(AndroID Java版本)没有开箱即用的约定,您可以自己轻松创建它:@H_502_0@interface Callback {voID act(List<AttendModel> models);}
@H_502_0@将它作为参数传递给firebase包装方法,并使用结果调用它.@H_502_0@public voID getAllAttendeesFor(String UUID, final ArrayList<AttendModel> attendarray, final Callback callback) { final DatabaseReference attendobjects = getDatabasetableWith(Constants.tableAttendobject); query queryRef = attendobjects.orderByChild(Constants.taAttendobjectUUID).equalTo(UUID); queryRef.addListenerForSingleValueEvent(new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) { HashMap<String, Object> attend = (HashMap<String, Object>) postsnapshot.getValue(); String UUID = (String) attend.get(Constants.AttendobjectUUID); String userUsername = (String) attend.get(Constants.AttendobjectUserUsername); AttendModel attendModel = new AttendModel(userUsername, UUID); attendarray.add(attendModel); callback.act(attendarray) } } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } });}
@H_502_0@然后在你的上层方法:@H_502_0@public voID getAttendants() { ArrayList<AttendModel> attendees = new ArrayList<AttendModel>(); FirebaseConnection.getInstance().getAllAttendeesFor(uuID, attendees, new Callable<List<AttendModel) { voID act(List<AttendModel> attendees){ //here you can update the UI Log.d("attendees", String.valueOf(attendees.size())); } };}
@H_502_0@请注意,只有在主线程上调用firebase查询的onDataChange方法时才能更新UI. 总结 以上是内存溢出为你收集整理的android – 处理异步任务(Firebase)返回的数据全部内容,希望文章能够帮你解决android – 处理异步任务(Firebase)返回的数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)