Попытка получить данные из firestore в режиме ресайклера - PullRequest
0 голосов
/ 12 июля 2020
  XML
 android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<TextView
    android:id="@+id/texttype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.118"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.132" />

<TextView
    android:id="@+id/textfats"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.6"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/texttype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.118"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.132" />

<TextView
    android:id="@+id/textfats"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.6"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Класс модели {

private String Type;
private String Fats;

public productsmodel(){}

public productsmodel(String Type, String Fats){
    this.Type = Type;
    this.Fats=Fats;
}


public String getType() {
    return Type;
}

public void setType(String type) {
    Type = type;
}

public String getFats() {
    return Fats;
}

public void setFats(String fats) {
    Fats = fats;
}

}

'

publi c class Products расширяет AppCompatActivity {

private RecyclerView products_list;
private FirebaseFirestore firebaseFirestore;
private FirestoreRecyclerAdapter adapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_products);
    products_list = findViewById(R.id.products_list);
    //Query
    Query query = firebaseFirestore.collection("Products");

    //Recycler options
    FirestoreRecyclerOptions<productsmodel> options = new FirestoreRecyclerOptions.Builder<productsmodel>().setQuery(query,productsmodel.class).build();
    adapter = new FirestoreRecyclerAdapter<productsmodel, productsviewholder>(options) {
        @NonNull
        @Override
        public productsviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singleitemlist,parent,false);
            return new productsviewholder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull productsviewholder holder, int position, @NonNull productsmodel model) {
            holder.Fats.setText(model.getFats()+"");
            holder.Type.setText(model.getType()+"");

        }
    };
    products_list.setLayoutManager(new LinearLayoutManager(this));
    products_list.setAdapter(adapter);

    }

private class productsviewholder extends RecyclerView.ViewHolder  {
    private TextView Type;
    private TextView Fats;
    public productsviewholder(@NonNull View itemView) {
        super(itemView);
        setContentView(R.layout.activity_products);
        Type=itemView.findViewById(R.id.textfats);
        Fats=itemView.findViewById(R.id.texttype);
    }
}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}

} `

1 Ответ

0 голосов
/ 12 июля 2020

Просто добавьте

FirebaseFirestore firebase = FirebaseFirestore.getInstance()

в свой метод onCreate перед использованием firebase.

...