gpt4 book ai didi

java - Firebase 模型 POJO getter 在 FirebaseRecyclerAdapter 中返回 null

转载 作者:行者123 更新时间:2023-11-29 23:16:50 25 4
gpt4 key购买 nike

我有一个 FirebaseRecyclerAdapter 从 firebase 数据库中获取数据,但是当我尝试访问 firebase 数据时,POJO 的 getter 方法返回 null。我能够获取数据库引用 key 。

    final Query beveragesQuery = mDatabaseReference.child(FirebaseValues.PRODUCTS)
.child(FirebaseValues.BEVERAGES);
FirebaseRecyclerOptions<GenericProductModel> beveragesOptions =
new FirebaseRecyclerOptions.Builder<GenericProductModel>()
.setQuery(beveragesQuery, GenericProductModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<GenericProductModel, Combos.MovieViewHolder>(
beveragesOptions
) {
@NonNull
@Override
public Combos.MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_cardview_layout, parent, false);
return new Combos.MovieViewHolder(view);
}

@Override
protected void onBindViewHolder(@NonNull Combos.MovieViewHolder viewHolder, int position, @NonNull GenericProductModel model) {
Log.d(TAG, "Item received:"+getRef(position).getKey());
String json = new Gson().toJson(model);
Log.d(TAG, "Item received:"+ json);
Log.d(TAG, "Item received:"+ model.toString());
if (tv_no_item.getVisibility() == View.VISIBLE) {
tv_no_item.setVisibility(View.GONE);
}
Log.d(TAG, "card name:"+model.getCardname());
viewHolder.cardname.setText(model.getCardname());
viewHolder.cardprice.setText("₹ " + Float.toString(model.getCardprice()));
Picasso.get().load(model.getCardimage()).into(viewHolder.cardimage);

viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Beverages.this, IndividualProduct.class);
intent.putExtra("product", getItem(position));
startActivity(intent);
}
});
}
@Override
public void onError(DatabaseError e) {
Log.e(TAG, "RV Adapter, Error occurred: " + e.getMessage());
}
};
mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(mLayoutManager);
adapter.startListening();
mRecyclerView.setAdapter(adapter);
}

我的 POJO 或模型类是

public class GenericProductModel implements Serializable {

public int cardid;
public String cardname;
public String cardimage;
public String carddescription;
public float cardprice;

public GenericProductModel() {
}

public GenericProductModel(int cardid, String cardname, String cardimage, String carddescription, float cardprice) {
this.cardid = cardid;
this.cardname = cardname;
this.cardimage = cardimage;
this.carddescription = carddescription;
this.cardprice = cardprice;
}

public int getCardid() {
return cardid;
}

public String getCardname() {
return cardname;
}

public String getCardimage() {
return cardimage;
}

public String getCarddescription() {
return carddescription;
}

public float getCardprice() {
return cardprice;
}

public void setCardid(int cardid) {
this.cardid = cardid;
}

public void setCardname(String cardname) {
this.cardname = cardname;
}

public void setCardimage(String cardimage) {
this.cardimage = cardimage;
}

public void setCarddescription(String carddescription) {
this.carddescription = carddescription;
}

public void setCardprice(float cardprice) {
this.cardprice = cardprice;
}
}

我正在实现 Serializable,因为我将此数据作为 Intent 发送给其他 Activity 。

为清楚起见添加了更多日志选项当我运行应用程序时,我得到的日志输出是:

03-17 15:05:55.200 4501-4501/com.vdeveloper.chaisutta D/BeveragesTAG: Item received, received:1
03-17 15:05:55.227 4501-4501/com.vdeveloper.chaisutta D/BeveragesTAG: Item received:{"a":0,"e":0.0}
03-17 15:05:55.227 4501-4501/com.vdeveloper.chaisutta D/BeveragesTAG: Item received:com.vdeveloper.chaisutta.b.a@63d3fc
03-17 15:05:55.227 4501-4501/com.vdeveloper.chaisutta D/BeveragesTAG: card name:null

数据库截图: enter image description here

最佳答案

我自己找到了解决办法。问题出在我的 POJO 上。由于这个项目是在 androidx 上,我需要添加注释“@Keep”来阻止编译器删除它认为多余的方法。

import java.io.Serializable;

import androidx.annotation.Keep;

@Keep
public class GenericProductModel implements Serializable {

public int cardid;
public String cardname;
public String cardimage;
public String carddescription;
public float cardprice;

public GenericProductModel() {
}

public GenericProductModel(int cardid, String cardname, String cardimage, String carddescription, float cardprice) {
this.cardid = cardid;
this.cardname = cardname;
this.cardimage = cardimage;
this.carddescription = carddescription;
this.cardprice = cardprice;
}

public int getCardid() {
return cardid;
}

public String getCardname() {
return cardname;
}

public String getCardimage() {
return cardimage;
}

public String getCarddescription() {
return carddescription;
}

public float getCardprice() {
return cardprice;
}

public void setCardid(int cardid) {
this.cardid = cardid;
}

public void setCardname(String cardname) {
this.cardname = cardname;
}

public void setCardimage(String cardimage) {
this.cardimage = cardimage;
}

public void setCarddescription(String carddescription) {
this.carddescription = carddescription;
}

public void setCardprice(float cardprice) {
this.cardprice = cardprice;
}
}

谢谢大家的帮助

关于java - Firebase 模型 POJO getter 在 FirebaseRecyclerAdapter 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55196002/

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