2017-03-31 22 views
0

我想要做的是按id排序數據。我知道我應該使用booksInstance.orderByChild(「id」)。我試過這樣做,沒有什麼改變,因爲我不知道應該在代碼中使用它。這裏是我如何顯示數據。在firebase數據庫中按ID排序android

private void getData(){ 
     firebaseDatabaseInstance = FirebaseDatabase.getInstance(); 
     // get reference to 'users' node 
     booksInstance = firebaseDatabaseInstance.getReference("a3lamAlda3wa"); 
     books.clear(); 
     books.addAll(db.getAllA3()); 
     adapter = new BookGridAdapter(this, books); 
     gridView.setAdapter(adapter); 
     if (books.isEmpty()) { 
      Constants.showLoadingDialog(this); 
     } 
     booksInstance.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       storeData(dataSnapshot); 
      } 
      @Override 
      public void onCancelled(DatabaseError error) { 
       // Failed to read value 
       Log.w(TAG, "Failed to read value.", error.toException()); 
      } 
     }); 
     booksInstance.orderByChild("id").addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       BookData book = new BookData(

         (String) dataSnapshot.child("id").getValue(), 
         (String) dataSnapshot.child("book_name").getValue(), 
         (String)dataSnapshot.child("book_path").getValue(), 
         (String)dataSnapshot.child("book_path2").getValue(), 
         "", 
         (String)dataSnapshot.child("image_path").getValue(), 
         (String)dataSnapshot.child("image_path2").getValue(), 
         "" 
       ); 
       db.insertA3(book); 
       reloadData(); 
      } 
     }); 
    } 
     private void storeData(DataSnapshot snapshot) { 
     books.clear(); 
     for (DataSnapshot alert: snapshot.getChildren()) { 
      BookData book = new BookData(
        (String)alert.child("id").getValue(), 
        (String)alert.child("book_name").getValue(), 
        (String)alert.child("book_path").getValue(), 
        (String)alert.child("book_path2").getValue(), 
        "", 
        (String)alert.child("image_path").getValue(), 
        (String)alert.child("image_path2").getValue(), 
        "" 
      ); 
      db.insertBook(book); 
     } 
     books.addAll(db.getAllBook()); 
     adapter.notifyDataSetChanged(); 
    } 

這是我的數據庫看起來像

"books" : [ null, { 
     "book_name" : "book 1", 
     "book_path" : "gs://appspot.com/books/1.pdf", 
     "book_path2" : "https://firebasestorage.googleapis.com", 
     "id" : "a-1", 
     "image_path" : "gs://mostsharabdallah-c0c65.appspot.com", 
     "image_path2" : "https://firebasestorage.googleapis.com" 
     } 

回答

0

而不是你的聽衆附加書的位置,您將它連接到命令查詢。所以:

booksInstance.orderByChild("id").addChildEventListener(new ChildEventListener() { 

出於好奇:既然你的書有標識已經,你爲什麼不單純將它們存儲在他們的密鑰ID?

+0

我想這樣做,但它仍然不是ID – Khallad

+0

我貼的是你的代碼的問題之一訂購它。可能還有更多,但這絕對是一個。使用[重現您現在卡住的最小代碼]更新您的問題(http://stackoverflow.com/help/mcve)。還包括您正在查詢的JSON(作爲文本)。您可以通過單擊[您的Firebase數據庫控制檯](https://console.firebase.google.com/project/_/database/data/)中的導出JSON鏈接輕鬆獲取此信息。將JSON作爲文本進行搜索可以讓我們輕鬆地使用它來測試您的實際數據並在我們的答案中使用它,並且通常是一件好事。 –

+0

我已經更新了這個問題,並且包含了JSON作爲文本 – Khallad