public class from_the_database {
public String last_entry(String object) {
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
final String ok = object;
final ArrayList<String> list = new ArrayList<String>(); // array to save result
Query query = database.child("records").orderByChild("object").equalTo(object);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
Data_model_milk dmm =
post.getValue(Data_model_milk.class);
Log.e("value",dmm.getDate()); // this works though
list.add("item was added last at " + dmm.getDate()); // not being added
}
} else {
list.add("no insert for " + ok); // not being added
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("from the db cancelled", databaseError.toString());
}
});
Log.e(object, list.get(0)); // not available
return list.get(0); // list.get(0) is empty
}
}
所以问题是 dmm.getDate()返回一个值,但未保存在数组列表或需要由主函数last_entry返回的任何其他变量中
我使用了以0,1和10大小初始化的最终数组,但似乎没有任何作用。
答案 0 :(得分:1)
您正在采取的方法,将无法奏效。我猜您正在尝试使您的类成为实用程序类。
您应该更改方法,可能下面的代码可以提供帮助
public class from_the_database {
public void last_entry(String object, OnDataChangeCallback listener) {
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
final String ok = object;
Query query = database.child("records").orderByChild("object").equalTo(object);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
Data_model_milk dmm =
post.getValue(Data_model_milk.class);
Log.e("value",dmm.getDate()); // this works though
listener.onDataChange("item was added last at " + dmm.getDate());
}
} else {
listener.onDataChange("no insert for " + ok);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("from the db cancelled", databaseError.toString());
}
});
}
public interface OnDataChangeCallback{
void onDataChange(String data);
}
}
现在如下调用last_entry方法
last_entry("queryString", new OnDataChangeCallback(){
public void onDataChange(String data){
// do something with data
}
});
答案 1 :(得分:0)
只有在基类和内部类中都存在相同的对象名称时,您的代码中出现的情况才会发生
public class LockTest {
public static void main(String[] args) {
final ArrayList<String> strList = new ArrayList<>(); // Same object name variable in base-class and inner-class
Hello hello = new Hello() {
@Override
public void run() {
strList.add("Hello");
}
};
System.out.println(strList.size()); // Output is 0
}
}
interface Hello {
ArrayList<String> strList = new ArrayList<>();
void run();
}
在您的情况下,这似乎不太可能,但请看一下这位天使提出的问题。也许是这样
答案 2 :(得分:0)
onDataChange
是asynchronous
,所以您的
Log.e(object, list.get(0)); // not available
return list.get(0); // list.get(0) is empty
将在调用onDataChange
之前执行,因此您的列表始终为空。
因此,与其从函数中返回一个字符串,不如创建一个带有字符串参数的新函数并像这样在onDataChange
内部调用该函数,效果更好:
public void last_entry(String object) {
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
final String ok = object;
final ArrayList<String> list = new ArrayList<String>(); // array to save result
Query query = database.child("records").orderByChild("object").equalTo(object);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
Data_model_milk dmm =
post.getValue(Data_model_milk.class);
Log.e("value",dmm.getDate()); // this works though
list.add("item was added last at " + dmm.getDate()); // not being added
}
} else {
list.add("no insert for " + ok); // not being added
}
performSomeAction(list.get(0));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("from the db cancelled", databaseError.toString());
}
});
}
public void performSomeAction(String msg){
//Do your action here
}