如何解决RecyclerView:没有附加适配器;跳过布局找不到问题
我 1 天前问过同样的问题,但我的问题被关闭了,所以我再问一次。
不知道为什么显示“未连接适配器”
我在 showProducts() 中间设置了布局管理器和回收器视图;方法我还将适配器设置为回收站视图,但仍然说:“没有连接适配器”找不到问题出在哪里,我很乐意接受一些帮助。谢谢各位!
主要活动:
公共类 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 extends 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) {
}
});
}
,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,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);
}
确保像我一样在 onCreate
中初始化适配器,并在 onDataChange
中为您的列表执行此操作
`Product product = new Product(id,image);
products.add(product);
productAdapter.notifyDataSetChanged();`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。