Adrixb
Adrixb

Reputation: 153

RecyclerView: No adapter attached; skipping layout (can't find issue)

I had ask same question 1 day ago and my question got closed so i ask it again.

Don't know why it says "No adapter attached"

I set the layout manager and the recycler view, in middle of showProducts(); method i also set the adapter to the recycler view and still says: "No adapter attached" Can't find where is the issue, i'll gladly accept some help. Thanks folks!

Nothing shows on screen for the following code:

Main activity:

public class MainActivity extends 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) {

        }


    });


}

}

Adapter:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{

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);
}

}

Upvotes: 2

Views: 100

Answers (2)

The Codepreneur
The Codepreneur

Reputation: 244

ProductAdapter productAdapter;

 @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);

      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) {


        }
    });


    layoutManager = new LinearLayoutManager(this);

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



    showProducts();


    registerForContextMenu(rView);
    
    
}

make sure you initialize your adapter in your onCreate as I did and for your list do this in the onDataChange

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

Upvotes: 1

Anwar Elsayed
Anwar Elsayed

Reputation: 493

Move this line inside if condition rView.setAdapter(productAdapter); in your showProducts function like below

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) {

    }


});

}

Upvotes: 1

Related Questions