首页 > 解决方案 > 错误:无法将 java.lang.String 类型的值转换为 int

问题描述

我刚刚开始使用 Java for Android 进行编程,当我运行我的应用程序时,程序就崩溃了。我知道有一个错误,但我不明白我在哪里错过了从“String”到“Int”的转换(com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 int)。

下面是我认为我有转换错误的代码。

private void DisplayAllProducts() {

            Query sort_product_in_descending_order = products_reference.orderByChild("counter");

                    FirebaseRecyclerOptions<products> options = new FirebaseRecyclerOptions.Builder<products>().setQuery(sort_product_in_descending_order, products.class).build();

        adapter_products = new FirebaseRecyclerAdapter<products, ProductsViewHolder>(options) {

                @Override
                protected void onBindViewHolder(@NonNull ProductsViewHolder productsViewHolder, int position, @NonNull products products) {

                    final String ProductsKey = getRef(position).getKey();

                    productsViewHolder.setFullname(products.getFullname());
                    productsViewHolder.setID(products.getID());
                    productsViewHolder.setCollection(products.getCollection());
                    productsViewHolder.setIndustrialCode(products.getIndustrialcode());
                    productsViewHolder.setQuality(products.getQuality());
                    productsViewHolder.setDesign(products.getDesign());
                    productsViewHolder.setColor(products.getColor());
                    productsViewHolder.setWeight_grs_m2(products.getWeight_grs_m2());
                    productsViewHolder.setWeight_grs_ml(products.getWeight_grs_ml());
                    productsViewHolder.setWidth(products.getWidth());
                    productsViewHolder.setPrice(products.getPrice());
                    productsViewHolder.setGeneralComposition(products.getGeneralcomposition());
                    productsViewHolder.setPileComposition(products.getPilecomposition());
                    //productsViewHolder.setProfileImage(getApplicationContext(), products.getProfileimage());
                    //productsViewHolder.setProductImage(getApplicationContext(), products.getProductimage());
                    productsViewHolder.setDate(products.getDate());
                    productsViewHolder.setTime(products.getTime());

                    productsViewHolder.mView.setOnClickListener(new View.OnClickListener(){ 

                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(activity_navigation.this, click_product.class);
                            i.putExtra("ProductsKey", ProductsKey);
                            startActivity(i);
                        }

                    });
    }


            @NonNull
            @Override
            public ProductsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_products_layout, parent, false);
                return new ProductsViewHolder(view);
            }
        };

            rv_navigation_productlist.setAdapter(adapter_products);
            adapter_products.startListening();

    }

标签: javaandroidfirebasefirebase-realtime-databasefirebaseui

解决方案


您可以使用Integer.parseInt("your_string")将字符串转换为整数。


推荐阅读