我遇到Firebase eventListeners的问题。我试图从Firebase数据库中获取一些数据,但我无法获得结果。问题是我的听众没有跑步或给出结果。我尝试使用2个不同的侦听器(onGetDataListener,ValueEventListener)获取结果。在调试期间,整个侦听器被跳过,调试器向前移动。我在所有监听器阶段都添加了Log.d但没有访问过。
public class Suggestionsfragment extends Fragment implements AdapterView.OnItemClickListener {
private GooglePlaceList nearby;
LinearLayout linearLayout;
Double latitude;
Double longitude;
List<String> urlList;
private List<String> interestList;
private List<Double> latlong;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.suggestions, container, false);
linearLayout = (LinearLayout) v.findViewById(R.id.main_layout);
String placesKey = this.getResources().getString(R.string.places_key);
interestList = new ArrayList<>();
latlong = new ArrayList<>();
nearby = null;
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
if (user != null) {
DatabaseReference ref1 = databaseReference.child(user.getUid()).child("interest");
readData(ref1, new OnGetDataListener() {
@Override
public void onSuccess(DataSnapshot dataSnapshot) {
Log.d("ONSUCCESS", "Success");
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String interests = postSnapshot.getValue(String.class);
interestList.add(interests);
}
}
@Override
public void onStart() {
//whatever you need to do onStart
Log.d("ONSTART", "Started");
}
@Override
public void onFailure() {
Log.d("ONFAILURE", "Failure");
}
});
databaseReference.child(user.getUid());
databaseReference.child("location");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("ONDATACHANGED", "listener2 entered");
try {
if (dataSnapshot.getValue() != null) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Double loc = postSnapshot.getValue(Double.class);
latlong.add(loc);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
latitude = latlong.get(0);
longitude = latlong.get(1);
int radius = 500;
urlList = generateUrlList(interestList, longitude, latitude, radius, placesKey);
if (placesKey.equals("PUT YOUR KEY HERE"))
Toast.makeText(getActivity(), "You haven't entered your Google Places Key into the strings file. Dont forget to set a referer too.", Toast.LENGTH_LONG).show();
else {
PlacesReadFeed process = new PlacesReadFeed();
process.execute(urlList);
}
return v;
}
...
}
答案 0 :(得分:1)
有许多事情需要解决。
此代码不对:
databaseReference.child(user.getUid()); // does nothing
databaseReference.child("location"); // does nothing
databaseReference.addListenerForSingleValueEvent(...);
你可能想要:
databaseReference.child(user.getUid()).child("location")
.addListenerForSingleValueEvent(...);
你说在调试过程中,整个监听器被跳过,调试器向前移动。这是因为在将查询发送到Firebase服务器并收到响应后,侦听器回调异步触发(更多详细信息here)。这意味着当这些语句执行时:
latitude = latlong.get(0);
longitude = latlong.get(1);
latlong
将不包含有效数据,因为onDataChange()
尚未执行。
如果您的听众从未开机,则可能是您没有互联网连接,或安全规则未设置为允许读取数据。解释了安全规则here。您应该将调试输出添加到onCancelled()
:
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("CANCELLED", databaseError.toException());
}
我不认识OnGetDataListener()
。这是你定义的一个类吗?
答案 1 :(得分:0)
我设法通过在onDataChanged中设置我的应用程序来处理Async firebase:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.suggestions, container, false);
linearLayout = (LinearLayout) v.findViewById(R.id.main_layout);
final String placesKey = this.getResources().getString(R.string.places_key);
interestList = new ArrayList<>();
latlong = new ArrayList<>();
nearby = null;
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
DatabaseReference ref1 = FirebaseDatabase.getInstance().getReference().child(user.getUid());
readData(ref1, new OnGetDataListener() {
@Override
public void onSuccess(DataSnapshot dataSnapshot) {
Log.d("ONSUCCESS", "Success");
for (DataSnapshot postSnapshot : dataSnapshot.child("interest").getChildren()) {
String interests = postSnapshot.getValue(String.class);
interestList.add(interests);
}
for (DataSnapshot postSnapshot : dataSnapshot.child("location").getChildren()) {
Double loc = postSnapshot.getValue(Double.class);
latlong.add(loc);
}
latitude = latlong.get(0);
longitude = latlong.get(1);
int radius = 500;
urlList = generateUrlList(interestList, longitude, latitude, radius, placesKey);
PlacesReadFeed process = new PlacesReadFeed();
process.execute(urlList);
}
@Override
public void onStart() {
//Whatever you want to do on start
Log.d("ONSTART", "Started");
}
@Override
public void onFailure() {
//Whatever you want to do in case of failure
Log.d("ONFAILURE", "Failure");
}
});
}
return v;
}
readData和OnGetListener接口:
public void readData(DatabaseReference ref, final OnGetDataListener listener) {
listener.onStart();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listener.onSuccess(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
listener.onFailure();
}
});
}
interface OnGetDataListener {
//make new interface for call back
void onSuccess(DataSnapshot dataSnapshot);
void onStart();
void onFailure();
}