2017-03-26 1 views
-1

나는 호스텔 목록과 이미지 및 시설이있는 사용 가능한 객실 유형을 표시하는 호스텔 안드로이드 응용 프로그램을 작성 중입니다.firebase 데이터베이스를 정규화하지 않고 다른 객체를 저장하고 링크를 유지하는 방법

나는 파이어 폭스가 그것을 낙담하기 때문에 데이터를 표준화하는 것을 피하기 위해 열심히 노력하고 있습니다.

문제 하나의 별개의 호스텔 객체를 firebase에 푸시하고 해당 방 객체와 연결된 상태로 유지하는 방법을 모르겠으므로 하나의 호스텔을 가져올 수 있으며 사용 가능한 룸이 시설을 포함하여 제공 될 수 있습니다. 그들은 가졌다.

이것은 내가하는 유혹이지만, 호스텔 배열의 객실과 시설을 중첩하는 것이 좋지 않다는 것을 알고 있습니다.

{ 
    "hostels":[ 
     { 
     "name":"Frontline Hostel", 
     "location":"Ayeduase", 
     "policy":"Security gates close at 11 30pm ...etc", 
     "views":"14", 
     "frontImage":"http://someUrl", 
     "roomsAvailable":[ 
      { 
       "numberOfOccupants":"3", 
      "price":"1800", 
      "remaining":"5", 
      "img1":"http://someUrl", 
      "img2":"http://someUrl", 
      "img3":"http://someUrl", 
      "img4":"http://someUrl" 
     }, 
     { 
      "numberOfOccupants":"3", 
      "price":"1800", 
      "remaining":"5", 
      "img1":"http://someUrl", 
      "img2":"http://someUrl", 
      "img3":"http://someUrl", 
      "img4":"http://someUrl" 
     } 
    ], 
    "facilities":[ 
     { 
      "shuttle":true 
     }, 
     { 
      "security cameras":true 
     }, 
     { 
      "free electricity":true 
     } 
    ] 
    }, 
    { 
    "name":"Evandy Hostel", 
    "location":"Bomso", 
    "policy":"Non-residents not allowed in hostel after 11pm ...etc", 
    "views":"8", 
    "frontImage":"http://someUrl", 
    "roomsAvailable":[ 
     { 
      "numberOfOccupants":"2", 
      "price":"2000", 
      "remaining":"7", 
      "img1":"http://someUrl", 
      "img2":"http://someUrl", 
      "img3":"http://someUrl", 
      "img4":"http://someUrl" 
     }, 
     { 
      "numberOfOccupants":"1", 
      "price":"3500", 
      "remaining":"2", 
      "img1":"http://someUrl", 
      "img2":"http://someUrl", 
      "img3":"http://someUrl", 
      "img4":"http://someUrl" 
     } 
    ], 
     "facilities":[ 
      { 
       "swimming pool":true 
      }, 
      { 
       "gym":true 
      }, 
      { 
       "wi-fi":true 
      } 
     ] 
     } 
    ] 
} 

나는 내가 중포 기지와 하나 호스텔 - 객실 시설을 인출 할 수 있도록 별개의 호스텔을 추가 할 수 있습니다 "시설 객체", 별도의 "숙소 객체", "공간 객체"를 모델링하는 방법에 대한 도움이 필요합니다 견해의 popuplation. 앱을 사용하기 전에 사용자가 인증하지 않아도됩니다. 개인용 버전을 사용하여 데이터베이스를 업데이트하고 최신으로 유지하는 방법이 필요합니다.

답변

0

@isaacllarbi이 코드를 작성하기 위해 Java를 사용하는 경우 해당 부모에 대해 다른 모델을 생성 할 것을 제안합니다. 노드 (귀하의 경우 이것은 호스텔, 객실 및 시설) 1) 호스텔 클래스의 경우이 번호를 사용하십시오

public class Host { String name, location, policy, frontImage; int보기;

public Hostels(String name, String location, String policy, String frontImage, int views) { 
    this.name = name; 
    this.location = location; 
    this.policy = policy; 
    this.frontImage = frontImage; 
    this.views = views; 
}}` 

루트에 데이터베이스 참조를 지정하여 호스텔 개체의 하위 항목을 추가하십시오. 두 개 이상의 호스텔을 추가하기 때문에, 당신이

public class Roomsavailable { 
String img1,img2,img3,img4; 
int numberofoccupants,price,remaining; 

public Roomsavailable(String img1, String img2, String img3, String img4, int numberofoccupants, int price, int remaining) { 
    this.img1 = img1; 
    this.img2 = img2; 
    this.img3 = img3; 
    this.img4 = img4; 
    this.numberofoccupants = numberofoccupants; 
    this.price = price; 
    this.remaining = remaining; 
} 

도 availale 객체 데이터베이스를함으로써 방에 아이를 추가 가능한 객실에 대한 각 숙소 또한

private void Hostelschildren (String name, String location, String policy, String frontImage, int views) { 
    Hostel host = new Hostel(name, location, policy, frontImage, views); 

    mDatabase.child("Hostels").child(hostelid).setValue(host); 
} 

에 대한 고유 ID를 생성하는 것이 중요합니다 루트 (이 경우에는 사용 가능한 방)에 대한 참조. 두 개 이상의 객실을 추가하고 있기 때문에, 당신이 마지막에 아이를 추가

각 방에 추가

private void roomsdata (String img1, String img2 String img3, String img4, int numberofoccupants, int price, int remaining) { 
    Roomsavailable rooms = new Roomsavailable(img1, img2, img3, img4, numberofoccupants, price, remaining); 

    mDatabase.child("Hostels").child("rooms available").child(roomid).setValue(rooms); 
} 

} 과 시설

public class Facilities { 
String swimmingpool, gym, wifi; 

public Facilities(String swimmingpool, String gym, String wifi) { 
    this.swimmingpool = swimmingpool; 
    this.gym = gym; 
    this.wifi = wifi; 
} 

}에 대한 고유 ID를 생성하는 것이 중요합니다 호스텔 노드 아래의 노드 (이 경우에는 rooms facilities)에 대한 데이터베이스 참조를 작성하여 Facilities 객체.

private void facilities (String gym, String swimming_pool, String wifi) { 
    Facilities facili = new Facilities(gym, swimming_pool, wifi); 

    mDatabase.child("Hostels").child(hostelid).child("facilities").setValue(facili); 
} 

참고 데이터베이스 참조하고 해당 장소 고유 ID 년대를 추가 할 수 있습니다. 도움이 되었기를 바랍니다. 귀하의 의견을 남겨주세요.

+0

정답은 @ XY-JOE에게 감사드립니다. 3 개의 객체 (호스텔, 객실, 시설)를 서로 연결되도록 유지하기 위해 전달해야하는 고유 ID를 생성하는 방법 'mDatabase.child ("호스텔"). 자식 (hostelid) .setValue (호스트) ;' 'mDatabase.child ("호스텔") 자녀 ("roomsavailable") 자녀 (roomid) .setValue (객실)...' 'mDatabase.child ("호스텔") 자녀 (hostelid) .child ("facilities"). setValue (facili); ' – isaacllarbi

+0

ID는 이름, 날짜 또는 그 밖의 고유 한 것이 될 수 있습니다. 앞에서 말했던 것처럼 수동으로 데이터를 저장하려고하기 때문에 ID는 새로운 데이터가 기존 데이터를 덮어 쓰지 않도록합니다. 호스텔 이름이 다를 것이라는 확신이들 경우, ID로 날짜순으로 이름을 사용하는 것이 좋습니다. 코드를 처리 할 수 ​​있는지 또는 코드가 필요한지 알려주십시오. 그런데 위의 답이 도움이된다면, 그 질문이 답을 나타내 었는지 확인해야합니다 @isaacllarbi –

관련 문제