java - java.lang.ClassCastException: java.lang.String 无法转换为 java.lang.Long 错误
问题描述
public class DBqueries {
public static FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
public static List<CategoryModel> categoryModelList = new ArrayList<>();
public static List<HomePageModel> homePageModelList = new ArrayList<>();
public static void loadCategories(final CategoryAdapter categoryAdapter, final Context context){
firebaseFirestore.collection("CATEGORIES").orderBy("index").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot documentSnapshot : task.getResult()){
categoryModelList.add(new CategoryModel(documentSnapshot.get("icon").toString(),documentSnapshot.get("categoryName").toString()));
}
categoryAdapter.notifyDataSetChanged();
}else {
String error = task.getException().getMessage();
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
}
}
});
}
public static void loadFragmentData(final HomePageAdapter adapter, final Context context){
firebaseFirestore.collection("CATEGORIES")
.document("HOME")
.collection("TOP_DEALS").orderBy("index").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot documentSnapshot : task.getResult()){
if ((long)documentSnapshot.get("view_type") == 0){
List<SliderModel> sliderModelList = new ArrayList<>();
long no_of_banners = (long) documentSnapshot.get("no_of_banners");
for (long x = 1;x < no_of_banners + 1;x++){
sliderModelList.add(new SliderModel((String) documentSnapshot.get("banner_"+x)
,documentSnapshot.get("banner_"+x+"_background").toString()));
}
homePageModelList.add(new HomePageModel(0,sliderModelList));
}else if ((long)documentSnapshot.get("view_type") == 1){
homePageModelList.add(new HomePageModel(1, (String) documentSnapshot.get("strip_ad_banner"),
documentSnapshot.get("background").toString()));
}else if ((long)documentSnapshot.get("view_type") == 2){
List<WishlistModel> viewAllProductList = new ArrayList<>();
List<HorizontalProductScrollModel> horizontalProductScrollModelList = new ArrayList<>();
long no_of_products = (long) documentSnapshot.get("no_of_products");
for (long x = 1;x < no_of_products + 1;x++){
horizontalProductScrollModelList.add(new HorizontalProductScrollModel(documentSnapshot.get("product_ID_"+x).toString()
,documentSnapshot.get("product_image_"+x).toString()
,documentSnapshot.get("product_title_"+x).toString()
,documentSnapshot.get("product_subtitle_"+x).toString()
,documentSnapshot.get("product_price_"+x).toString()));
viewAllProductList.add(new WishlistModel(documentSnapshot.get("product_image_"+x).toString()
,documentSnapshot.get("product_full_title_"+x).toString()
,(long)documentSnapshot.get("free_coupons_"+x)
,documentSnapshot.get("average_rating_"+x).toString()
,(long)documentSnapshot.get("total_ratings_"+x)
,documentSnapshot.get("product_price_"+x).toString()
,documentSnapshot.get("cutted_price_"+x).toString()
,(boolean)documentSnapshot.get("COD_"+x)));
}
homePageModelList.add(new HomePageModel(2,documentSnapshot.get("layout_title").toString()
,documentSnapshot.get("layout_background").toString(),
horizontalProductScrollModelList,viewAllProductList));
}else if ((long)documentSnapshot.get("view_type") == 3){
List<HorizontalProductScrollModel> gridLayoutModelList = new ArrayList<>();
long no_of_products = (long) documentSnapshot.get("no_of_products");
for (long x = 1;x < no_of_products + 1;x++){
gridLayoutModelList.add(new HorizontalProductScrollModel(documentSnapshot.get("product_ID_"+x).toString()
,documentSnapshot.get("product_image_"+x).toString()
,documentSnapshot.get("product_title_"+x).toString()
,documentSnapshot.get("product_subtitle_"+x).toString()
,documentSnapshot.get("product_price_"+x).toString()));
}
homePageModelList.add(new HomePageModel(3,documentSnapshot.get("layout_title").toString()
,documentSnapshot.get("layout_background").toString(),
gridLayoutModelList));
}
}
adapter.notifyDataSetChanged();
}else {
String error = task.getException().getMessage();
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
}
}
});
}
}
当我运行代码时,我得到了这个异常:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
有没有其他方法可以转换结果?
解决方案
使用QueryDocumentSnapshot
(继承自DocumentSnapshot
)提供的正确 API 方法,而不是(不正确)手动将结果转换为正确的类型。
推荐阅读
- java - 如何使用 api Graphstream 在 android studio 中通过触摸创建节点?
- gtk - monodevelop gtk-sharp 中的 MDI 形式
- java - [TreeMap]:在一个类中创建和加载 TreeMap,并从几个单独/不同的类中访问该 Tmap
- javascript - 如何在不占用空间的情况下隐藏 html 元素 x 时间?
- python-3.x - Mac终端flake8 --version在pip3下载后找不到flake8
- c# - List<>.Append 不适用于公共、静态和初始化字段
- r - R - ggplot2:基于数据类别的曲线下的阴影区域
- algorithm - 如何证明一般多项式情况下的 Big-omega?
- c++ - 相同的未更改代码是否有可能有时运行而有时不运行?
- node.js - 无法使用 ssl 和 apache 连接到 websocket