2017-03-17 59 views
0

我的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; 
    } 
    ... 
} 

回答

0

我設法與異步工作通過建立我的onDataChanged內應用火力取:

@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(); 
} 
1

有很多事情要解決。

此代碼是不正確的:

databaseReference.child(user.getUid()); // does nothing 
databaseReference.child("location"); // does nothing 
databaseReference.addListenerForSingleValueEvent(...); 

你可能想:

databaseReference.child(user.getUid()).child("location") 
    .addListenerForSingleValueEvent(...); 

你說在調試整個聽衆越來越跳過,調試器前進。這是因爲在將查詢發送到Firebase服務器並收到響應之後,偵聽器回調將異步觸發(更多詳細信息,請參閱here)。這意味着,當這些語句執行:因爲onDataChange()尚未執行

latitude = latlong.get(0); 
longitude = latlong.get(1); 

latlong將不包含有效數據。

如果您的聽衆不會觸發,則可能是您沒有互聯網連接,或者安全規則未設置爲允許讀取數據。安全規則解釋爲here。你應該調試輸出添加到onCancelled()

@Override 
public void onCancelled(DatabaseError databaseError) { 
    Log.e("CANCELLED", databaseError.toException()); 
} 

我不認識OnGetDataListener()。這是你定義的類嗎?

+0

我曾經有過datareference爲你指示沒有結果:databaseReference.child(user.getUid())。child(「location」) .addListenerForSingleValueEvent(...); 我已經找到了onGetDataListener從這個帖子: http://stackoverflow.com/questions/42621911/firebase-ondatachange-not-entered 這似乎與我的問題,但它並沒有黯然幫助。 –

+0

你說得對,異步監聽器回調可能是問題。我已經註釋了可能導致空指針的變量,並且onDataChanged在執行後運行3秒。我想我需要找出一種在代碼執行之前檢索Firebase數據的方法。 –