마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

파이어베이스 데이터 가져오기 도와주세요

0 추천

파이어베이스에 여러 유저들이 있고 그 유저들이 각각 가지고 있는 데이터 들을 모두 가져오려합니다.

아래 코드로 진행해보았지만 아무런 값이 나오지 않습니다. 어떤게 문제인지 지적해주실 수 있나요?

 DatabaseReference myRef = database.getReference("users");
        myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    LocationList mfriendLocation = dataSnapshot.getValue(LocationList.class);

                    friendLocation.add(mfriendLocation);

                    for(int i = 0 ; i < friendLocation.size(); i++){
                        LocationList newFriendLoaction  = new LocationList();
                        newFriendLoaction.setEmail(friendLocation.get(i).email);
                        newFriendLoaction.setsLatitude(friendLocation.get(i).sLatitude);
                        newFriendLoaction.setsLongitude(friendLocation.get(i).sLongitude);
                        newFriendLoaction.setdLatitude(friendLocation.get(i).dLatitude);
                        newFriendLoaction.setdLatitude(friendLocation.get(i).dLatitude);

                        stOtherUser = newFriendLoaction.getEmail();
                        makeText(getActivity(),stOtherUser,Toast.LENGTH_SHORT).show();

                        slatitude = newFriendLoaction.getsLatitude();
                        slongitude = newFriendLoaction.getsLongitude();
                        dlatitude = newFriendLoaction.getdLatitude();
                        dlongitude = newFriendLoaction.getdLongitude();


                        stSLocation = GetAddress(geocoder,slatitude,slongitude);
                        stDLocation = GetAddress(geocoder,dlatitude,dlongitude);
                        setOthersCurrentLocation(slatitude,slongitude,stOtherUser,"",stSLocation,stDLocation,newFriendLoaction.email);


                    }

                }
            }

 

뚜덩 (120 포인트) 님이 2017년 12월 18일 질문
Firebase에 대해 잘 알지 못하지만, 단순히 코드만 보고 질문 드립니다.

1. friendLocation를 for 루프 하는 이유가 뭔지 알 수 있을까요?
mfriendLocation를 friendLocation에 추가하였다가 다시 for 루프 하는 이유를 잘 모르겠네요.

2. LocationList mfriendLocation = dataSnapshot.getValue(LocationList.class);는
LocationList mfriendLocation = snapshot.getValue(LocationList.class); 으로 바꾸는게 맞지 않을까 생각됩니다.
앗 지적 감사합니다. 제가 데이터베이스를 제대로 배워본적없이 안드로이드로 다루려다보니 너무 어렵네요ㅠㅠ

2번 지적 감사합니다.

1번 지적해주신것은 제가 List<LocationList > friendLocation; 으로 선언하여 모든 각 항목의 값에 접근하려고

for루프를 이용했습니다. 게터 세터는 값이 제대로 들어갔는지 확인하려고 넣어봤다가 어쩌다 보니 모든 항목에 대해 적용을 해놨네요;;

지금은 다른방법으로 데이터 접근하여 나름 문제를 해결 했습니다. 지적 감사합니다!

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...