在RecylcerView中显示Firestore集合的问题

时间:2018-03-28 15:19:20

标签: java android firebase android-recyclerview google-cloud-firestore

我正在尝试在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()));
                    }
                }
            }
        });
    }
}

Car.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数组是null并且我修复了它,现在当我加载活动时没有任何事情发生我认为可能影响它的唯一的事情是在调试器中。

[Debug warning[1]

除此之外,我眼中没有明显的问题。我们非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

您会收到这些错误,因为在您的模型类中,您的字段是使用小写字母声明的,并且在您的数据库中存在首字母大写字母。这就是你得到的原因:

No setter/field for Year on...

请参阅Year首字母Y。为了解决这个问题,您有两种解决方案。首先是删除实际数据并添加新数据,因此模型类中的字段与数据库中的字段相同或使用注释,方法是在每个字段前面添加以下注释:

@PropertyName("Year")
private String year