recyclerView E/RecyclerView: 没有连接适配器;跳过布局

recyclerView E/RecyclerView: No adapter attached; skipping layout

不知道为什么显示“未连接适配器”

我在 showProducts() 中间设置了布局管理器和回收器视图;方法我还将适配器设置为回收站视图,但仍然说:“未连接适配器”找不到问题出在哪里,我很乐意接受一些帮助。谢谢大家!

以下代码在屏幕上不显示任何内容:

主要activity:

public class MainActivity 扩展 AppCompatActivity {

private RecyclerView rView;
private RecyclerView.LayoutManager layoutManager;
private List<Product> products;
private ProductAdapter productAdapter;
private DatabaseReference mDataBase;
private CardView cardView;




//Product
private String id;
private String name;
private String author;
private String genre;
private String description;
private double  price;
private int stock;
private String image;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    products = new ArrayList<Product>();

    mDataBase = FirebaseDatabase.getInstance().getReference();
    cardView = (CardView) findViewById(R.id.cardView);

    layoutManager = new LinearLayoutManager(this);

    rView = (RecyclerView) findViewById(R.id.recyclerView);
    rView.setHasFixedSize(true);
    rView.setLayoutManager(layoutManager);



    showProducts();


    registerForContextMenu(rView);

}

private void showProducts() {

    mDataBase.child("Products").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {
                products.clear();
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    name = ds.child("title").getValue().toString();
                    author = ds.child("author").getValue().toString();
                    genre = ds.child("genre").getValue().toString();
                    description = ds.child("description").getValue().toString();
                    price = Double.parseDouble(ds.child("price").getValue().toString());
                    stock = Integer.parseInt(ds.child("stock").getValue().toString());
                    image = ds.child("image").getValue().toString();
                    id = ds.getKey();

                    Product product = new Product(id, name, author, genre, description, price, stock,  image);
                    products.add(product);

                }
                productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(Product product, int position) {



                    }
                }, new ProductAdapter.OnButtonClickListener() {
                    @Override
                    public void onButtonClick(Product product, int position) {


                    }
                });
            }
            rView.setAdapter(productAdapter);

        }
            // Observa como pasamos el activity, con this. Podríamos declarar
            // Activity o Context en el constructor y funcionaría pasando el mismo valor, this


        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }


    });


}

}

适配器:

public class ProductAdapter 扩展 RecyclerView.Adapter{

private Context context;
private List<Product> products;
private int layout;
private OnItemClickListener itemClickListener;
private OnButtonClickListener btnClickListener;


public ProductAdapter(List<Product> products, int layout, OnItemClickListener itemListener, OnButtonClickListener btnListener) {
    this.products = products;
    this.layout = layout;
    this.itemClickListener = itemListener;
    this.btnClickListener = btnListener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
    context = parent.getContext();
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(products.get(position), itemClickListener, btnClickListener);
}

@Override
public int getItemCount() {
    return products.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView name;
    public TextView author;
    public TextView genre;
    public TextView description;
    public TextView price;
    public TextView stock;
    public ImageView image;
    public Button btnDelete;


    public ViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.textViewProductName);
        author = (TextView) itemView.findViewById(R.id.textViewProductAuthor);
        genre = (TextView) itemView.findViewById(R.id.textViewProductGenre);
        description = (TextView) itemView.findViewById(R.id.textViewProductDescription);
        price = (TextView) itemView.findViewById(R.id.textViewProductPrice);
        stock = (TextView) itemView.findViewById(R.id.textViewProductStock);
        image = (ImageView) itemView.findViewById(R.id.imageViewProduct);
        btnDelete = (Button) itemView.findViewById(R.id.buttonDeleteCity);
    }

    public void bind(final Product product, final OnItemClickListener itemListener, final OnButtonClickListener btnListener) {
        name.setText(product.getName());
        description.setText(product.getDescription());
        price.setText((int) product.getPrice());
        Picasso.get().load(product.getImage()).fit().into(image);

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnListener.onButtonClick(product, getAdapterPosition());
            }
        });


        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemListener.onItemClick(product, getAdapterPosition());
            }
        });
    }
}

public interface OnItemClickListener {
    void onItemClick(Product product, int position);
}

public interface OnButtonClickListener {
    void onButtonClick(Product product, int position);
}

}

在onCreate中绑定Adapter

这样使用

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    products = new ArrayList<Product>();

    mDataBase = FirebaseDatabase.getInstance().getReference();
    cardView = (CardView) findViewById(R.id.cardView);

    layoutManager = new LinearLayoutManager(this);

    rView = (RecyclerView) findViewById(R.id.recyclerView);
    rView.setHasFixedSize(true);
    rView.setLayoutManager(layoutManager);
    rView.setAdapter(productAdapter);




    showProducts();


    registerForContextMenu(rView);

}

private void showProducts() {

    mDataBase.child("Products").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {
                products.clear();
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    name = ds.child("title").getValue().toString();
                    author = ds.child("author").getValue().toString();
                    genre = ds.child("genre").getValue().toString();
                    description = ds.child("description").getValue().toString();
                    price = Double.parseDouble(ds.child("price").getValue().toString());
                    stock = Integer.parseInt(ds.child("stock").getValue().toString());
                    image = ds.child("image").getValue().toString();
                    id = ds.getKey();

                    Product product = new Product(id, name, author, genre, description, price, stock,  image);
                    products.add(product);

                }
                productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(Product product, int position) {



                    }
                }, new ProductAdapter.OnButtonClickListener() {
                    @Override
                    public void onButtonClick(Product product, int position) {


                    }
                });
            }
           
            productAdapter.notifyDataStateChanged();

        }


        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }


    });


}