2016-12-05 5 views
1

나는이 내 firebaseRecyclerAdapter하고 이것은 재활용 어댑터와 함께 백업액세스 자식 노드는

Query queryRef = chatRef.orderByKey(); 
queryRef.addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot snapshot, String previousChild) { 
     System.out.println(snapshot.getKey()); 

     for (DataSnapshot snapshot1 : snapshot.getChildren()) { 
      final String firstname = snapshot.getKey(); 
      chatViewHolder.setName(firstname); 

      chatViewHolder.mView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        //Log.w(TAG, "You clicked on "+position); 
        //String firstname = tList.getFirstname(); 
        //String lastname = tList.getLastname(); 
        //firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase 
        //lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here 
        //String name = (firstname + " " + lastname); // concatenate firstname and lastname variable. 

        Intent intent = new Intent(getActivity(), MainChat.class); //change to onclick 
        intent.putExtra("fullname", firstname); 
        //you can name the keys whatever you like 
        //intent.putExtra("image", userList.getImgUrl().toString()); //note that all these values have to be primitive (i.e boolean, int, double, String, etc.) 
        startActivity(intent); 

       } 
      }); 
     } 

다음과 같이 populateViewHolder이고 내 데이터베이스는

users 
    uid 
     conversations 
        sLname + sFname 
          -KyF........: 
            message: hi 
            timestamp: ServerValue.timestamp 
          -KyFr......: 
            message:hello 
            timestamp: ServerValue.timestamp 

구현과 같이 보입니다 firebaseRecyclerAdapter

RecyclerView recycler = (RecyclerView) rootView.findViewById(R.id.recyclerview4); 
recycler.setHasFixedSize(true); 
recycler.setLayoutManager(new LinearLayoutManager(getActivity())); 


FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<ChatList, ChatHolder>(ChatList.class, R.layout.chatlistrow, ChatHolder.class, chatRef) { 
    @Override 
    public void populateViewHolder(final ChatHolder chatViewHolder, final ChatList chatList, final int position) { 

     //try catch block to catch events of no posts, it will most likely return a null error, so I'm catching it, else 
     //find its exception and catch itef 

     contactList = new ArrayList<String>(); 

     //start here and getkey for everyone 

     contactList.add(chatRef.getKey()); 

     //String firstname = chatRef.getKey(); 
     //chatViewHolder.setName(firstname); 

     Query queryRef = chatRef.orderByKey(); 
     queryRef.addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot snapshot, String previousChild) { 
       System.out.println(snapshot.getKey()); 

       for (DataSnapshot snapshot1 : snapshot.getChildren()) { 
        final String firstname = snapshot.getKey(); 
        chatViewHolder.setName(firstname); 

        chatViewHolder.mView.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          //Log.w(TAG, "You clicked on "+position); 

          Intent intent = new Intent(getActivity(), MainChat.class); //change to onclick 
          intent.putExtra("fullname", firstname); 
          //you can name the keys whatever you like 
          //intent.putExtra("image", userList.getImgUrl().toString()); //note that all these values have to be primitive (i.e boolean, int, double, String, etc.) 
          startActivity(intent); 
         } 
        }); 
       } 
       //testing 
      } 

      @Override 
      public void onChildChanged(DataSnapshot snapshot, String previousChild) { 
      } 

      @Override 
      public void onChildMoved(DataSnapshot snapshot, String previousChild) { 
      } 

      @Override 
      public void onChildRemoved(DataSnapshot snapshot) { 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
      } 
      // .... 
     }); 
    } 
}; 
recycler.setAdapter(mAdapter); 

my ex port.json는

"admin" : { 
    "ZpMBDchTkPVh7El3EsIKEdmV28n1" : { 
     "bio" : "", 
     "conversations" : { 
     "Chinwendu Chiebidolu" : { 
      "-KYCaWdm7IJrF-yau0ES" : { 
      "message" : "hello", 
      "rfirstname" : "Chinwendu", 
      "rimgUrl" : "", 
      "rlastname" : "Chiebidolu", 
      "rname" : "Chinwendu Chiebidolu", 
      "timestamp" : 1480918181229, 
      "userId" : "lgsmNM2LXuVq8NBoDwDCaoe5B613" 
      } 
     }, 
     "Damilola Aromiwe" : { 
      "-KYBzfTc5xHCPGM06QIE" : { 
      "message" : "hi", 
      "rfirstname" : "Damilola", 
      "rimgUrl" : "2124", 
      "rlastname" : "Aromiwe", 
      "rname" : "Damilola Aromiwe", 
      "timestamp" : 1480907995851, 
      "userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2" 
      }, 
      "-KYCYYkifzI-61dBKsTT" : { 
      "message" : "hello", 
      "rfirstname" : "Damilola", 
      "rimgUrl" : "2124", 
      "rlastname" : "Aromiwe", 
      "rname" : "Damilola Aromiwe", 
      "timestamp" : 1480917404523, 
      "userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2" 
      }, 
      "-KYCZ19NM2yKinU5eSLt" : { 
      "message" : "hello mr dammy, are you there? ", 
      "rfirstname" : "Damilola", 
      "rimgUrl" : "2124", 
      "rlastname" : "Aromiwe", 
      "rname" : "Damilola Aromiwe", 
      "timestamp" : 1480917527734, 
      "userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2" 
      } 
     } 
     }, 
     "email" : "[email protected]", 
     "firstname" : "Toyosi", 
     "imgUrl" : "2124", 
     "lastname" : "Yusuf", 
     "name" : "toyosi", 
     "parent" : true, 
     "phone" : "", 
     "teacher" : false, 
     "userId" : "ZpMBDchTkPVh7El3EsIKEdmV28n1" 
    } 
    }, 
    "teachers" : { 
    "inFa52OMArdKyUTMYrkRqWs50uD2" : { 
     "bio" : "", 
     "email" : "[email protected]", 
     "firstname" : "David", 
     "imgUrl" : "", 
     "lastname" : "Akinyemi", 
     "name" : "", 
     "parent" : false, 
     "phone" : "", 
     "teacher" : true, 
     "userId" : "inFa52OMArdKyUTMYrkRqWs50uD2" 
    }, 
    "xITyS4TrbWXPBhHYAgz0ZsdyYFa2" : { 
     "bio" : "", 
     "email" : "[email protected]", 
     "firstname" : "Ejura", 
     "imgUrl" : "", 
     "lastname" : "Ayeni", 
     "name" : "", 
     "parent" : false, 
     "phone" : "", 
     "teacher" : true, 
     "userId" : "xITyS4TrbWXPBhHYAgz0ZsdyYFa2" 
    } 
    }, 
    "users" : { 
    "HJ55v7xUGmXQN2o1AQFDG9JtzCy1" : { 
     "bio" : "", 
     "email" : "[email protected]", 
     "firstname" : "", 
     "imgUrl" : "", 
     "lastname" : "", 
     "name" : "", 
     "parent" : true, 
     "phone" : "", 
     "teacher" : false, 
     "userId" : "HJ55v7xUGmXQN2o1AQFDG9JtzCy1" 
    }, 
    "KrVp7D6gLoZoH6WcHT5tmyqb3o53" : { 
     "bio" : "", 
     "email" : "[email protected]", 
     "firstname" : "", 
     "imgUrl" : "", 
     "lastname" : "", 
     "name" : "", 
     "parent" : true, 
     "phone" : "", 
     "teacher" : false, 
     "userId" : "KrVp7D6gLoZoH6WcHT5tmyqb3o53" 
    }, 
    "lgsmNM2LXuVq8NBoDwDCaoe5B613" : { 
     "bio" : "", 
     "conversations" : { 
     "Toyosi Yusuf" : { 
      "-KYCaWdHGbWxsk8h7EWy" : { 
      "message" : "hello", 
      "sfirstname" : "Toyosi", 
      "simageurl" : "2124", 
      "slastname" : "Yusuf", 
      "timestamp" : 1480918179544 
      } 
     } 
     }, 
     "email" : "[email protected]", 
     "firstname" : "Chinwendu", 
     "imgUrl" : "", 
     "lastname" : "Chiebidolu", 
     "name" : "", 
     "parent" : true, 
     "phone" : "", 
     "teacher" : false, 
     "userId" : "lgsmNM2LXuVq8NBoDwDCaoe5B613" 
    }, 
    "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2" : { 
     "bio" : "", 
     "conversations" : { 
     "Toyosi Yusuf" : { 
      "-KYBzfT_p8N2oW7oqSke" : { 
      "message" : "hi", 
      "sfirstname" : "Toyosi", 
      "simageurl" : "2124", 
      "slastname" : "Yusuf", 
      "timestamp" : 1480907995531 
      }, 
      "-KYCYYkfORU70Y1_PGug" : { 
      "message" : "hello", 
      "sfirstname" : "Toyosi", 
      "simageurl" : "2124", 
      "slastname" : "Yusuf", 
      "timestamp" : 1480917401926 
      }, 
      "-KYCZ19KyX8X5G25CEz5" : { 
      "message" : "hello mr dammy, are you there? ", 
      "sfirstname" : "Toyosi", 
      "simageurl" : "2124", 
      "slastname" : "Yusuf", 
      "timestamp" : 1480917527335 
      } 
     } 
     }, 
     "email" : "[email protected]", 
     "firstname" : "Damilola", 
     "imgUrl" : "2124", 
     "lastname" : "Aromiwe", 
     "name" : "", 
     "parent" : true, 
     "phone" : "", 
     "teacher" : false, 
     "userId" : "ujNyb5erkSN7oS3aGZlXyc9Q0YJ2" 
    } 
    } 
} 

chatList.class()가 여기

package com.mordred.theschoolapp; 

/** 
* Created by mordred on 11/29/16. 
*/ 
public class ChatList { 

    public String userId; 
    public String rname; 
    public String rlastname; 
    public String rimageurl; 

    public ChatList() { 
     // Default constructor required for calls to DataSnapshot.getValue(User.class) 
    } 

    public String getFirstname() { 
     return rname; 
    } 

    public void setFirstname(String firstname) { 
     this.rname = firstname; 
    } 

    public String getLastname() { 
     return rlastname; 
    } 

    public void setLastname(String lastname) { 
     this.rlastname = lastname; 
    } 

    public String getImgUrl() { 
     return rimageurl; 
    } 

    public void setImgUrl(String imgUrl) { 
     this.rimageurl = imgUrl; 
    } 

    public String getUserId() { 
     return userId; 
    } 

    public void setUserId(String userId) { 
     this.userId = userId; 
    } 
} 

여기서 문제 가리키는 재활용 어댑터를 재귀 적으로 그 메시지를 검색하려고하는 메신저입니다. 그러나 위의 코드를 사용하면 첫 번째 메시지가 계속 반복됩니다. 아무도 나에게 무슨 일이 벌어지고 어떻게 해결할 수 있다고 말할 수 있습니까? 추가 정보가 필요하면 그냥 물어보십시오. 감사합니다.

+0

나는 당신의 문제를 이해할 수 없다. 'FirebaseRecyclerAdapter'의 구현을 게시하고, 어댑터가 어느 노드에서 데이터베이스를 청취하는지, 그리고 chatRef가 가리키는 노드를 지적 할 수 있습니까? 또한, 데이터베이스의'sLname + sFname'에서 무엇을 의미합니까? 정확한 JSON 형식을 게시하는 것이 좋습니다. Firebase 데이터베이스 콘솔에서 ** JSON 내보내기 **를 수행하십시오. – Wilik

+0

고맙습니다. 방금 – Mordred

+0

이라는 질문을 업데이트했습니다.이 노드에서'chatRef'가 'users//conversations/ /'을 가리키고 있습니까? – Wilik

답변

0

Firebase UI Database의 작동 방식을 모르는 것 같습니다. 전체 설명서는 here입니다. 어떻게 작동하는지 이해하려면 추가 정보를 읽어야합니다.

populateViewHolder 메서드는 onChildAdded/onChildChanged과 거의 동일하게 작동하여 참조 된 노드 아래에서 각 자식을 반환합니다.

그래서 당신의 ChatList 모델 클래스 그래서 어떤 방법이 null 값을 반환 할 수 있습니다 속성 또는 속성 데이터베이스의 키를 반영하지 않습니다이 빠져있는, 그런데이

chatRef = FirebaseDatabase.getInstance().getReference("users").child(uid).child("conversations"); 

... 

@Override 
public void populateViewHolder(final ChatHolder chatViewHolder, final ChatList chatList, final int position) { 
    contactList = new ArrayList<String>(); 
    // this is what you want 
    String fullName = mAdapter.getRef(position).getKey(); 
    contactList.add(fullName); 

    ... 
} 

처럼 보일 것입니다. Read and Write Data on Android

+0

고맙습니다. 그러나 나는 이것을 어떻게하는지 완벽하게 이해합니다, 그러나 여기서의 문제는 그것이 내가 돌아가고 싶은 것이 아니라는 것입니다. – Mordred

+0

대화의 직접적인 자녀를 돌려주고 싶습니다. 그 사람들이 아니야. – Mordred

+0

'chatRef'가'users//conversations /'not/users//conversations/ /'를 가리키고 있습니까? @Mordred – Wilik