java - com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.areef.pamur.Model.ClothingCategory 类型
问题描述
com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.areef.pamur.Model.ClothingCategory 类型
com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.areef.pamur.Model.ClothingCategory 类型
包 com.areef.pamur.EndUser;公共类 ClothingActivity 扩展 AppCompatActivity {
private DatabaseReference ClothingRef;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private String type = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_clothing);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ClothingRef = FirebaseDatabase.getInstance().getReference("Category").child("Clothing");
recyclerView = findViewById(R.id.clothing_category_list);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(layoutManager);
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<ClothingCategory> options =
new FirebaseRecyclerOptions.Builder<ClothingCategory>()
.setQuery(ClothingRef.orderByChild("name"), ClothingCategory.class).build();
FirebaseRecyclerAdapter<ClothingCategory, ClothingCategoryViewHolder> adapter =
new FirebaseRecyclerAdapter<ClothingCategory, ClothingCategoryViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ClothingCategoryViewHolder holder, int position, @NonNull final ClothingCategory model) {
holder.txtClothingCategorytName.setText(model.getName());
Picasso.get().load(model.getImage()).into(holder.clothingCategoryImageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (type.equals("Admin")) {
Intent intent = new Intent(ClothingActivity.this, AdminMaintainProductsActivity.class);
intent.putExtra("pid", model.getPid());
startActivity(intent);
} else if (model.getName().equals("Men")) {
Intent intent = new Intent(ClothingActivity.this, ClothingActivity.class);
intent.putExtra("name", model.getName());
startActivity(intent);
} else if (model.getName().equals("Women")) {
Intent intent = new Intent(ClothingActivity.this, ElectronicsActivity.class);
intent.putExtra("name", model.getName());
startActivity(intent);
} else if (model.getName().equals("Kids")) {
Intent intent = new Intent(ClothingActivity.this, FoodActivity.class);
intent.putExtra("name", model.getName());
startActivity(intent);
}
}
});
}
@NonNull
@Override
public ClothingCategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_clothing_items_layout, parent, false);
ClothingCategoryViewHolder holder = new ClothingCategoryViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
}
解决方案
因为您没有提供导致此异常的操作,所以我只能参考 Firebase 的文档来找出您的异常。
https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseException
但据我所见,您的代码存在问题。
该变量
type
在声明后保留为空字符串,因此您放置在onBindViewHolder()
反对中的条件语句type
不会生成输出Admin
该问题很可能是由您的 Firebase 结构和您尝试检索数据的方式引起的。
Clothing
您在asKids
&Men
和数据库参考中有孩子-ClothingRef = FirebaseDatabase.getInstance().getReference("Category").child("Clothing"); 您没有访问
Clothing
. 因此,您正在访问一个无效的数据集。
我的建议是为你的查询语句创建一个模型类Kids
并修改你的查询语句 ClothingRef = FirebaseDatabase.getInstance().getReference("Category").child("Clothing").child("Kids);
,看看你的输出是什么。
有关任何其他信息,请参阅实时数据库的官方 FirebaseUI 文档 - https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/
推荐阅读
- docker - 如何使用非 root 用户在 docker 中启动 crontab 作业
- java - 如何处理在网页上滚动时出现的硒形式?
- c - 未知的 CMake 命令“收集”
- java - 为什么我的矩形在我的 JPanel 中以一种奇怪的形式出现?
- javascript - 如何在socket.io中的断开事件上获取断开连接的客户端的套接字ID
- git - 控制狂:提交被拒绝。Bitbucket 中不允许 Foxtrot 合并
- javascript - 如何在 JavaScript 中正确回收缓冲区
- laravel - 合并两组数据,然后在laravel中排序和分页
- bash - 我必须每次运行 source ~/.bash_profile 才能看到 react-native
- php - var_dump() 不适用于 gRPC Protobuf 类的实例