我的Firebase eventListeners有問題。我正嘗試從Firebase數據庫獲取一些數據,但我無法獲得結果。問題是,我的聽衆沒有運行或返回結果。我試着用2個不同的監聽器獲取結果(onGetDataListener,ValueEventListener)。在調試過程中,整個偵聽器被跳過,調試器向前移動。我已經在所有偵聽器階段添加了Log.d,但沒有任何訪問。未輸入EventListeners
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;
}
...
}
我曾經有過datareference爲你指示沒有結果:databaseReference.child(user.getUid())。child(「location」) .addListenerForSingleValueEvent(...); 我已經找到了onGetDataListener從這個帖子: http://stackoverflow.com/questions/42621911/firebase-ondatachange-not-entered 這似乎與我的問題,但它並沒有黯然幫助。 –
你說得對,異步監聽器回調可能是問題。我已經註釋了可能導致空指針的變量,並且onDataChanged在執行後運行3秒。我想我需要找出一種在代碼執行之前檢索Firebase數據的方法。 –