Создал RecyclerView, адаптер, вроде все должно было работать но увы как всегда, не знаете почему может вылетать приложение при переключении на окно с RecyclerView. вот коды
package com.example.reston;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.reston.ui.ProductViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class ClientMenu extends AppCompatActivity {
DatabaseReference database;
RecyclerView recyclerView;
MyAdapter myAdapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_menu);
recyclerView = findViewById(R.id.dishViewer);
layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
database= FirebaseDatabase.getInstance().getReference("Dishes");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Dish> options = new FirebaseRecyclerOptions.Builder<Dish>()
.setQuery(FirebaseDatabase.getInstance("https://reston-bec54-default-rtdb.europe-west1.firebasedatabase.app/").getReference().child("Dishes"), Dish.class).build();
FirebaseRecyclerAdapter<Dish, ProductViewHolder> adapter = new FirebaseRecyclerAdapter<Dish, ProductViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull @NotNull ProductViewHolder holder, int i, @NonNull @NotNull Dish model) {
holder.dishName.setText(model.getName());
holder.dishDescription.setText(model.getDescription());
holder.dishPrice.setText(model.getPrice());
Picasso.get().load(model.getImage_id()).into(holder.imgDish);
}
@NonNull
@NotNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dish_template, parent, false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
public void onClickbtnBooking(View view){
Intent i = new Intent(ClientMenu.this, Booking.class);
startActivity(i);
finish();
}
}
package com.example.reston.ui;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.reston.R;
public class ProductViewHolder extends RecyclerView.ViewHolder implements ItemClickListnerr {
public TextView dishName, dishDescription, dishPrice;
public ImageView imgDish;
public ItemClickListner listner;
public ProductViewHolder(View itemView)
{
super(itemView);
imgDish = itemView.findViewById(R.id.imgDish);
dishName = itemView.findViewById(R.id.dishName);
dishDescription = itemView.findViewById(R.id.dishDescription);
dishPrice = itemView.findViewById(R.id.dishPrice);
}
public void setItemClickListner(ItemClickListner listner)
{
this.listner = listner;
}
@Override
public void onClick(View view)
{
listner.onClick(view, getAbsoluteAdapterPosition(), false);
}
}