【发布时间】:2018-03-28 15:19:20
【问题描述】:
我正在尝试在 RecylcerView 中显示 Firestore 集合,但无法在此处显示代码。
ViewCars.java:
public class ViewCars extends AppCompatActivity {
private RecyclerView mCarList;
private FirebaseFirestore firebaseFirestore;
private List<Car> carsList = new ArrayList<Car>();
private CarListAdapter carListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_cars);
carListAdapter = new CarListAdapter(carsList);
mCarList = (RecyclerView) findViewById(R.id.mainList);
mCarList.setHasFixedSize(true);
mCarList.setLayoutManager(new LinearLayoutManager(this));
mCarList.setAdapter(carListAdapter);
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("Cars").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for(DocumentChange doc: documentSnapshots.getDocumentChanges()){
if(doc.getType() == DocumentChange.Type.ADDED){
Car car = doc.getDocument().toObject(Car.class);
carsList.add(car);
carListAdapter.notifyDataSetChanged();
Log.d("Car List", Integer.toString(carsList.size()));
}
}
}
});
}
}
CarListAdapter:
public class ViewCars extends AppCompatActivity {
private RecyclerView mCarList;
private FirebaseFirestore firebaseFirestore;
private List<Car> carsList = new ArrayList<Car>();
private CarListAdapter carListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_cars);
carListAdapter = new CarListAdapter(carsList);
mCarList = (RecyclerView) findViewById(R.id.mainList);
mCarList.setHasFixedSize(true);
mCarList.setLayoutManager(new LinearLayoutManager(this));
mCarList.setAdapter(carListAdapter);
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("Cars").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for(DocumentChange doc: documentSnapshots.getDocumentChanges()){
if(doc.getType() == DocumentChange.Type.ADDED){
Car car = doc.getDocument().toObject(Car.class);
carsList.add(car);
carListAdapter.notifyDataSetChanged();
Log.d("Car List", Integer.toString(carsList.size()));
}
}
}
});
}
}
汽车.java:
public class Car {
private String id;
private String numPlate;
private String make;
private String model;
private String year;
private String price;
public Car(){
}
public Car(String id, String numPlate, String make, String model, String year, String price) {
this.setId(id);
this.setNumPlate(numPlate);
this.setMake(make);
this.setModel(model);
this.setYear(year);
this.setPrice(price);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNumPlate() {
return numPlate;
}
public void setNumPlate(String numPlate) {
this.numPlate = numPlate;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
最初我收到一个错误,说 carList 数组为空,我修复了这个问题,现在当我加载活动时没有任何反应,我认为唯一可能会影响它的是调试器中的这个。
[
除此之外,在我看来没有明确的问题。非常感谢您提供的任何帮助。
【问题讨论】:
标签: java android firebase android-recyclerview google-cloud-firestore