我正试图从Firebase实时数据库中检索数据(产品图片和价格),并将其放入回收器View中,但我遇到了一些困难。在我的数据库中,我存储了一个产品的图片url,我想把这个图片显示在放在cardView中的ImageView组件中。我也设法正确地检索了url和价格,但我的recyclerView没有显示它们。这是我的产品类。
public class CustomizeProduct {
private String productImage;
private String productName;
private String productPrice;
public CustomizeProduct(String image, String name, String price){
this.productImage = image;
this.productName = name;
this.productPrice = price;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage){
this.productImage = productImage;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName){
this.productName = productName;
}
public String getProductPrice() { return productPrice; }
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
}
我的适配器如下
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private ArrayList<CustomizeProduct> customized;
private Context myContext;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView thisProductImage;
public TextView thisProductName;
public TextView thisProductPrice;
public ViewHolder(View itemView) {
super(itemView);
thisProductImage = itemView.findViewById(R.id.customProductImage);
thisProductName = itemView.findViewById(R.id.customProductName);
thisProductPrice = itemView.findViewById(R.id.customProductPrice);
}
}
public ProductAdapter(Context context, ArrayList<CustomizeProduct> thisCustom) {
myContext = context;
customized = thisCustom;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredients, parent, false);
ViewHolder mv = new ViewHolder(v);
return mv;
}
public void onBindViewHolder(ViewHolder holder, int position) {
CustomizeProduct currentCustom = customized.get(position);
Picasso.get().load(currentCustom.getProductImage()).placeholder(R.drawable.shrimp_ditali).into(holder.thisProductImage);
holder.thisProductName.setText(currentCustom.getProductName());
holder.thisProductPrice.setText(currentCustom.getProductPrice());
}
public int getItemCount() {
return customized.size();
}
}
我把我的recyclerView放置在这个活动中。
public class BuildProduct extends AppCompatActivity {
public final static ArrayList<CustomizeProduct> customized = new ArrayList<>();
private RecyclerView recyclerView;
private ProductAdapter myAdapter;
private RecyclerView.LayoutManager layoutManager;
private static String productImage, productPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_build_product);
buildCustom();
}
public void buildCustom(){
recyclerView = findViewById(R.id.customList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("MENIU");
ref.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot ds) {
for (DataSnapshot menu : ds.getChildren()) {
String keys = menu.getKey();
String productName = (String) ds.child(keys).child("NUME_PRODUS").getValue();
if(productName.equals(checkout.thisProductName)){
productImage = ds.child(keys).child("IMAGINE").getValue(String.class);
Long price = (Long) ds.child(keys).child("PRET_PRODUS").getValue();
productPrice = String.valueOf(price);
Toast.makeText(getApplicationContext(), productImage + " " + productPrice, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
for(int i = 0; i < checkout.thisProductQty; i++) {
customized.add(new CustomizeProduct(productImage,checkout.thisProductName, productPrice));
myAdapter = new ProductAdapter(getApplicationContext(), customized);
}
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapter);
}
}
产品名称是正确的,因为它是一个变量, 但图像和价格没有任何变化。这个 "for "循环的作用是在我的产品结账时创建尽可能多的cardViews,我怎样才能让它显示数据库中的图片和价格呢?
这是我的Firebase子结构。
EDIT更新了代码。
public class CustomizeProduct {
private String imagine;
private String nume_produs;
private String pret_produs;
public CustomizeProduct(){}
public CustomizeProduct(String imagine, String nume_produs, String pret_produs){
this.imagine = imagine;
this.nume_produs = nume_produs;
this.pret_produs = pret_produs;
}
@PropertyName("IMAGINE")
public String getIMAGINE() {
return imagine;
}
@PropertyName("NUME_PRODUS")
public String getNUME_PRODUS() {
return nume_produs;
}
@PropertyName("PRET_PRODUS")
public String getPRET_PRODUS() { return pret_produs; }
}
在onBindViewHolder中,
public void onBindViewHolder(ViewHolder holder, int position) {
CustomizeProduct currentCustom = customized.get(position);
Picasso.get().load(currentCustom.getIMAGINE()).placeholder(R.drawable.shrimp_ditali).into(holder.thisProductImage);
holder.thisProductName.setText(currentCustom.getNUME_PRODUS());
holder.thisProductPrice.setText(currentCustom.getPRET_PRODUS());
}
customized.add(new CustomizeProduct(productImage,checkout.thisProductName, productPrice));
你只能看到产品名称(checkout.thisProductName
),但不是产品图片& 价格,因为你用产品名称填充你的适配器,而Firebase在你的 onDataChange(DataSnapshot ds)
回调;在你的代码中没有任何线索,你如何建立产品名称(checkout.thisProductName
)值。
现在,当你在主线程中构建适配器时,产品图片& 价格不显示,而firebase是在后台线程中隐式工作的,所以你需要将你的适配器同步到后台线程中,将构建recyclerView适配器的部分代码转移到firebase回调中,如下所示。
public void buildCustom(){
recyclerView = findViewById(R.id.customList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("MENIU");
ref.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot ds) {
for (DataSnapshot menu : ds.getChildren()) {
String keys = menu.getKey();
String productName = (String) ds.child(keys).child("NUME_PRODUS").getValue();
if(productName.equals(checkout.thisProductName)){
productImage = ds.child(keys).child("IMAGINE").getValue(String.class);
Long price = (Long) ds.child(keys).child("PRET_PRODUS").getValue();
productPrice = String.valueOf(price);
Toast.makeText(getApplicationContext(), productImage + " " + productPrice, Toast.LENGTH_LONG).show();
}
}
// here you've received all Firebase data and can now build the recyclerView
for(int i = 0; i < checkout.thisProductQty; i++) {
customized.add(new CustomizeProduct(productImage,checkout.thisProductName, productPrice));
myAdapter = new ProductAdapter(getApplicationContext(), customized);
}
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
我想你可能还需要注释私有字段,因为SDK使用这些字段来实现 设置 数据库中的值。
public class CustomizeProduct {
@PropertyName("IMAGINE")
private String imagine;
@PropertyName("NUME_PRODUS")
private String nume_produs;
@PropertyName("PRET_PRODUS")
private String pret_produs;
public CustomizeProduct(){}
public CustomizeProduct(String imagine, String nume_produs, String pret_produs){
this.imagine = imagine;
this.nume_produs = nume_produs;
this.pret_produs = pret_produs;
}
@PropertyName("IMAGINE")
public String getIMAGINE() {
return imagine;
}
@PropertyName("NUME_PRODUS")
public String getNUME_PRODUS() {
return nume_produs;
}
@PropertyName("PRET_PRODUS")
public String getPRET_PRODUS() { return pret_produs; }
}
请注意,使用getters和setters是完全可选的,你也可以使用这个(短得多)的类来绑定到同一个JSON结构。
public class CustomizeProduct {
@PropertyName("IMAGINE")
public String imagine;
@PropertyName("NUME_PRODUS")
public String nume_produs;
@PropertyName("PRET_PRODUS")
public String pret_produs;
}