2017-03-31 11 views
0

idでデータを並べ替えるだけです。私はbooksInstance.orderByChild( "id")を使うべきであることを知っています。私はそれをやってみたが、何が変わったのかは、コード内でどこで使うべきかわからないからです。ここで私はどのようにデータを表示しています。ファイヤーベースデータベースの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

どのように見えるかです。あなたの本はすでにIDを持っているので、IDとしてキーの下に保管するのはなぜ簡単ではないのですか?

+0

私はそれをやってみましたが、まだIDで注文していません – Khallad

+0

投稿した内容は、あなたのコードの問題の1つです。もっとあるかもしれないが、これは間違いなく1つだった。 [あなたが今どこにいるのかを再現する最小限のコード]で質問を更新してください(http://stackoverflow.com/help/mcve)。また、照会しているJSONもテキストとして含めます。 [Firebaseデータベースコンソール](https://console.firebase.google.com/project/_/database/data/)の[Export JSON]リンクをクリックすると、簡単にこれを取得できます。 JSONをテキストとして使用すると検索が可能になり、実際のデータを使ってテストしたり、答えに使用したりすることができます。一般的には良いことです。 –

+0

質問を更新し、JSONをテキストとして追加しました – Khallad

関連する問題