0

Error caused

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4198dda0)

E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.unimaseshop, PID: 21195

java.lang.NumberFormatException: Invalid int: "40.00"

Due to the section at onBindViewHolder; it couldn't run the java activity.

package com.example.ecommerce;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.unimaseshop.Model.CartModel;
import com.example.unimaseshop.Prevalage.Prevalage;
import com.example.unimaseshop.ViewHolder.CartViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Cart extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private Button NextProcessBtn;
    private TextView txtTotalAmount;
    private int overTotalPrice = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        recyclerView = findViewById(R.id.cart_list);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        NextProcessBtn = (Button) findViewById(R.id.next_process_btn);
        txtTotalAmount = (TextView) findViewById(R.id.total_price);

        NextProcessBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtTotalAmount.setText("Total Price = RM" + String.valueOf(overTotalPrice));

                Intent intent = new Intent(Cart.this, ConfirmFinalOrder.class);
                intent.putExtra("Total Price", String.valueOf(overTotalPrice));
                startActivity(intent);
                finish();
            }
        });

    }

    @Override
    protected void onStart()

    {
        super.onStart();
        txtTotalAmount.setText("Total Price = RM" + String.valueOf(overTotalPrice));


        final DatabaseReference cartListRef = FirebaseDatabase.getInstance().getReference().child("Cart List");
        FirebaseRecyclerOptions<CartModel> options =
                new FirebaseRecyclerOptions.Builder<CartModel>()
                        .setQuery(cartListRef.child("User View").child(Prevalage.currentOnlineUser.getPhone())
                                .child("Products"),CartModel.class).build();

        FirebaseRecyclerAdapter<CartModel, CartViewHolder> adapter
              = new FirebaseRecyclerAdapter<CartModel, CartViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull CartViewHolder holder, int position, @NonNull final CartModel model)
            {
              holder.txtProductQuantity.setText("Quantity " + model.getQuantity());
                holder.txtProductPrice.setText("Price = RM" + model.getPrice());
                holder.txtProductName.setText(model.getPname());

                ***int oneTypeProductPrice = ((Integer.valueOf(model.getPrice()))) * Integer.valueOf(model.getQuantity());***
                overTotalPrice = overTotalPrice + oneTypeProductPrice;

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CharSequence options[]= new CharSequence[]
                                {
                                  "Edit",
                                  "Remove",
                                };
                        AlertDialog.Builder builder = new AlertDialog.Builder(Cart.this);
                        builder.setTitle("Cart Options:");

                        builder.setItems(options, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                if (i == 0)
                                {
                                    Intent intent = new Intent(Cart.this,ProductDetailActivity.class);
                                    intent.putExtra("pid", model.getPid());
                                    startActivity(intent);
                                }
                                if(i==1)

                                {
                                    cartListRef.child("User View")
                                            .child(Prevalage.currentOnlineUser.getPhone())
                                            .child("Products")
                                            .child(model.getPid())
                                            .removeValue()
                                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    if(task.isSuccessful())
                                                    {
                                                        Toast.makeText(Cart.this, "Item Removed Successfully", Toast.LENGTH_SHORT).show();
                                                        Intent intent = new Intent(Cart.this,Home.class);
                                                        startActivity(intent);
                                                    }
                                                }
                                            });

                                }
                            }
                        });

                        builder.show();
                    }
                });
            }

            @NonNull
            @Override
            public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
            {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout, parent, false);
                CartViewHolder holder = new CartViewHolder(view);
                return holder;
            }
        };

        recyclerView.setAdapter(adapter);
        adapter.startListening();
    }

}
Charmaine Yeap
  • 55
  • 1
  • 10

1 Answers1

0

use

(int) ParseDouble(model.getPrice())

instead of

Integer.valueOf(model.getPrice())

You can use this func.

double ParseDouble(String value) {
    if (value != null && !value.isEmpty()) {
        try {
            return Double.parseDouble(value);
        } catch(Exception e) {
            return 0.0;
        }
    }
    else return 0.0;
}
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35