【问题标题】:Issue with displaying Firestore collection in RecylcerView在 RecyclerView 中显示 Firestore 集合的问题
【发布时间】: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


    【解决方案1】:

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

    No setter/field for Year on...
    

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

    @PropertyName("Year")
    private String year
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 2021-08-13
      相关资源
      最近更新 更多