gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:52:07 25 4
gpt4 key购买 nike

1天前我问过同样的问题,我的问题被关闭了,所以我再问一次。
不知道为什么它说“未连接适配器”
我在 showProducts() 中间设置了布局管理器和回收器 View ;方法我还将适配器设置为回收站 View ,但仍然说:“未连接适配器”找不到问题所在,我很乐意接受一些帮助。谢谢各位!
以下代码的屏幕上没有显示任何内容:
主要 Activity :
公共(public)类 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)类 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);
}
}

最佳答案

如果条件 rView.setAdapter(productAdapter); 将这条线移到里面在您的 showProducts 功能
像下面

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

}


});
}

关于android - RecyclerView : No adapter attached; skipping layout (can't find issue),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66908073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com