我正在尝试从FirebaseDatabase导入数据并且它正在工作但不能使用此类,当我尝试获取数据时它会给我一个空值,这就是它所谓的RoomsAvailable类可以告诉我为什么我得到空值,我该如何解决它。
public class RoomsAvailable {
private int onePerson;
private int twoPerson;
private int threePerson;
private int fourPerson;
public RoomsAvailable() {
}
public RoomsAvailable(int onePerson, int twoPerson, int threePerson, int fourPerson) {
this.onePerson = onePerson;
this.twoPerson = twoPerson;
this.threePerson = threePerson;
this.fourPerson = fourPerson;
}
public int getOnePerson() {
return onePerson;
}
public void setOnePerson(int onePerson) {
this.onePerson = onePerson;
}
public int getTwoPerson() {
return twoPerson;
}
public void setTwoPerson(int twoPerson) {
this.twoPerson = twoPerson;
}
public int getThreePerson() {
return threePerson;
}
public void setThreePerson(int threePerson) {
this.threePerson = threePerson;
}
public int getFourPerson() {
return fourPerson;
}
public void setFourPerson(int fourPerson) {
this.fourPerson = fourPerson;
}
@Override
public String toString() {
return "RoomsAvailable{" +
"onePerson=" + onePerson +
", twoPerson=" + twoPerson +
", threePerson=" + threePerson +
", fourPerson=" + fourPerson +
'}';
}
}
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<Hotel> hotelList = new ArrayList<>();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.child("0/"+"hotels").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (int i = 0; i < dataSnapshot.getChildrenCount(); i++) {
Hotel hotel = dataSnapshot.child(i + "").getValue(Hotel.class);
hotelList.add(hotel);
Log.d(TAG, hotel.getId() + "\n");
Log.d(TAG, hotel.getCityId() + "\n");
Log.d(TAG, hotel.getName() + "\n");
Log.d(TAG, hotel.getRoomsAvailable() + "\n");
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cities_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
RecyclerView.Adapter adapter = new HotelAdapter(getApplicationContext(), hotelList);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCreate:Failed to read hotelList.", databaseError.toException());
}
});
}
这是我的Firebase数据库
问:问题是什么?问:我该如何解决?
答案 0 :(得分:0)
您总是null
,因为您声明了hotelList
外onDataChange()
方法。这是由于此方法的异步行为而发生的,甚至在将这些酒店添加到列表之前就会调用此方法。因此,为了解决这个问题,请在onDataChange()
内移动列表声明,或者如果您想在外面使用它,请参阅我的答案表格post。
您的代码应如下所示:
mDatabaseReference.child("0/"+"hotels").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<Hotel> hotelList = new ArrayList<>();
for (int i = 0; i < dataSnapshot.getChildrenCount(); i++) {
Hotel hotel = dataSnapshot.child(i + "").getValue(Hotel.class);
hotelList.add(hotel);
Log.d(TAG, hotel.getId() + "\n");
Log.d(TAG, hotel.getCityId() + "\n");
Log.d(TAG, hotel.getName() + "\n");
Log.d(TAG, hotel.getRoomsAvailable() + "\n");
}
Log.d("TAG", hotelList);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cities_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
RecyclerView.Adapter adapter = new HotelAdapter(getApplicationContext(), hotelList);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCreate:Failed to read hotelList.", databaseError.toException());
}
});
答案 1 :(得分:0)
为什么不尝试这样
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<Hotel> hotelList = new ArrayList<>();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.child("0/"+"hotels").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
hotelList = (List<Hotel>) dataSnapshot.getValue(Hotel.class);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cities_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
RecyclerView.Adapter adapter = new HotelAdapter(getApplicationContext(), hotelList);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCreate:Failed to read hotelList.", databaseError.toException());
}
});
}