package com.iko.android.firebase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.security.PrivateKey;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public class Lokasi{
private String wisata1;
private String wisata2;
public Lokasi(){ }
public Lokasi(String wisata1, String wisata2) {
this.wisata1 = wisata1;
this.wisata2 = wisata2;
}
public String getWisata1() {
return wisata1;
}
public String getWisata2() {
return wisata2;
}
public void setWisata1(String wisata1) {
this.wisata1 = wisata1;
}
public void setWisata2(String wisata2) {
this.wisata2 = wisata2;
}
}
ArrayList<Lokasi> list;
FirebaseDatabase mDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("lokasi");
ChildEventListener childEventListener = myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Lokasi value = dataSnapshot.child(s).getValue(Lokasi.class);
//
Log.d("MyReports", value.getWisata1() + value.getWisata2());
Log.d("MyReports", dataSnapshot.getValue().toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
{
}
});
}
}
这是我每次运行时的代码 它在LogCat上没有给我任何东西
但是当我删除这个
Lokasi value = dataSnapshot.child(s).getValue(Lokasi.class);
//
Log.d("MyReports", value.getWisata1() + value.getWisata2());
的输出
Log.d("MyReports", dataSnapshot.getValue().toString());
出来给我字符串
似乎我做错了将快照传递给Object
public class Lokasi{
private String wisata1;
private String wisata2;
public Lokasi(){ }
public Lokasi(String wisata1, String wisata2) {
this.wisata1 = wisata1;
this.wisata2 = wisata2;
}
public String getWisata1() {
return wisata1;
}
public String getWisata2() {
return wisata2;
}
public void setWisata1(String wisata1) {
this.wisata1 = wisata1;
}
public void setWisata2(String wisata2) {
this.wisata2 = wisata2;
}
}
这是我的类代码,这是我的FireBase结构
{
"Reports" : {
"Random ID" : {
"wisata1" : "bumijo",
"wisata2" : "malioboro"
}
},
"desa" : {
"Deskripsi" : {
"deskripsi1" : "Tempat Iko Tinggal",
"deskripsi2" : "Tempat Adel Berada"
},
"PemilikHotel1" : "Iko",
"PemilikHotel2" : "Adel",
"alamat" : {
"alamat1" : "Jln Bumijo",
"alamat2" : "Jln Malioboro"
},
"email" : {
"email1" : "iko@gmail.com",
"email2" : "adel@gmail.com"
},
"fasilHotel1" : {
"fasilHotel11" : "toilet",
"fasilHotel12" : "kasur"
},
"fasilHotel2" : {
"fasilHotel21" : "toilet"
},
"hotel" : {
"hotel1" : "Bumijo Residence",
"hotel2" : "Malioboro Residence"
},
"lokasi" : {
"wisata1" : "Jln Bumijo",
"wisata2" : "Jln Malioboro"
},
"telpon" : {
"telpon1" : 565619,
"telpon2" : 454518
}
},
"message" : "Hello, World!"
}
IDE始终显示value.getWisata1()
可能会产生java.lang.NullPointerException
并且错误说
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.iko.android.firebase.MainActivity$Lokasi
答案 0 :(得分:0)
问题是我将Class Lokasi
放在主要活动中,因此该值不希望uupdated到构造函数