请考虑下面的代码片段:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
现在这段代码不起作用,我只能通过回调来解决这个问题。我不希望这样。我想要一个返回ArrayList的方法。我怎么能改写这个?使用Kotlin也是一种选择。
答案 0 :(得分:0)
在这段代码中,我正在检索该节点中可用的所有行:
//全局变量\ ArrayList arrayListEventName = new ArrayList();
public ArrayList<String or Pojo>getEventNames() {
progressDialog();
//----Shared Preference----\\
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String emailId = prefs.getString(StaticValues.PREF_EMAIL, "name@provider,com");
emailId = emailId.replace(".", ",");
//--------------\\
database = FirebaseDatabase.getInstance(); // Database Instance \\
DatabaseReference databaseReference_users = database.getReference("admin").child(emailId)
.child("eventsCreated");
databaseReference_users.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(final com.google.firebase.database.DataSnapshot dataSnapshot) {
arrayListEventName.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String eventName = (String) postSnapshot.child("eventName").getValue(String.class).toLowerCase();
// if(eventName.equals(eventName2)){
//
// }
arrayListEventName.add(eventName);
}
dismissDialog();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
返回arrayListEventName;
}
截图:
答案 1 :(得分:0)
public Task<ArrayList<GenericModel>> getList(GenericModel model) {
if (!mIsInitialized) {
initialize();
}
final TaskCompletionSource<ArrayList<GenericModel>> tcs = new TaskCompletionSource<>();
if (mIsInitialized) {
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<GenericModel> genericModels = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
GenericModel genericModel = postSnapshot.getValue(model.getClass());
genericModels.add(genericModel);
}
Collections.sort(genericModels, (o1, o2) -> o1.getDate().compareTo(o2.getDate()));
tcs.setResult(genericModels);
}
@Override
public void onCancelled(DatabaseError databaseError) {
tcs.setResult(null);
}
};
mDatabase.child(model.getTableName()).child(mUser.getUid()).addListenerForSingleValueEvent(postListener);
}
return tcs.getTask();
}
这里是调用方法:( Kotlin,有共同例程)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
doAsync {
val task = FirebaseManager.getInstance().getList(FixedCosts())
task.addOnCompleteListener {
uiThread {
if (task.result != null) {
//show the result in UI
}
}
}
}