Firebase에 일부 객체를 저장하려고했지만 이후 액세스하지만 액세스하는 부분에 문제가 있습니다. 하나의 객체를 저장하고 그것을 검색 할 수는 있지만, .push()를 사용하면 여러 객체를 조작하는 방법을 알 수 없습니다..push()를 사용하여 Firebase에 저장된 객체에 액세스하려고 시도합니다.
지금까지 아무 것도 시도하지 않았지만 설명서가 실제로 도움이되지 않았습니다. 기본적으로, 나는 2 개의 단추 및 TextView가있다. 하나의 버튼은 객체의 인스턴스를 생성하고이를 Firebase에 추가하고 다른 객체는 TextView에 객체의 일부 데이터를 표시합니다.
public void endWorkout(View view) {
DatabaseReference dbRef = database.getReference();
dbRef.child("workouts").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Workout firstWorkout = dataSnapshot.getValue(Workout.class);
TextView textView = (TextView) findViewById(R.id.test_text_view_Workouts);
textView.setText(firstWorkout.getLocation());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "firebase call didn't work!");
}
});
}
여기에 JSON은 중포 기지에서 (필자는 중포 기지의 값의 일부를 편집이다 :
public void startWorkout(View view) {
DatabaseReference dbRef = database.getReference();
Workout workout = new Workout(10,30,20,"The Gym");
dbRef.child("workouts")
.push()
.setValue(workout);
}
이이 개체를 가져올 수있는 방법입니다 :
이
개체를 추가하는 방법 콘솔이므로 값이 정확히 같지 않은 것이 정상입니다.) :{
"workouts" : {
"-KKdzxVp9DDOpBsvhU3_" : {
"endTime" : 30,
"location" : "Home",
"startTime" : 10,
"totalTime" : 20
},
"-KKdzy13b6_tyaiQbW9W" : {
"endTime" : 30,
"location" : "The Gym",
"startTime" : 10,
"totalTime" : 20
},
"-KKe--wkPRQYWhcCBpyF" : {
"endTime" : 30,
"location" : "The Gym",
"startTime" : 10,
"totalTime" : 20
}
}
}
다음은
public class Workout {
// Declaring variables needed to store workout data
public long startTime;
public long endTime;
public long totalTime;
public String location;
// Empty Constructor required for Firebase
public Workout(){
}
public Workout(long startTime, long endTime, long totalTime, String location) {
this.startTime = startTime;
this.endTime = endTime;
this.totalTime = totalTime;
this.location = location;
}
// Calculate totalTime
public void calculateTotalTime(){
this.totalTime = endTime - startTime;
}
// Getters and Setters
public long getStartTime() {
return this.startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getEndTime() {
return this.endTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
public long getTotalTime() {
return this.totalTime;
}
public void setTotalTime(long totalTime) {
this.totalTime = totalTime;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
}
내가 프로그래밍 비교적 새로운 해요, 그래서 내가 얻을 수있는 모든 도움을 주셔서 감사합니다 것 : 운동 클래스는 내가 만들었습니다. 감사!
: http://stackoverflow.com/questions/32886546/how firebase-android 일종의 일을 처리 했으므로 객체의 데이터에 액세스 할 수 있지만 시뮬레이션 방법을 알 수는 없었습니다. 해당 솔루션을 사용하여 객체 목록을 얻으십시오. – JCLaHoot