-1

Im trying to pass a list of cart item from 1 activity to another activity so that user can have 1 last view about the order before they check out. the problem is when i run it there's an error java.util.ArrayList cannot be cast to android.os.Parcelable can somebody help me with this problem ?

This is the intent code

    Intent i = new Intent(CreateNewReceiptActivity.this,ViewFinalReceiptActivity.class);
                    i.putExtra("receiptcart", (Parcelable) itemList);
                startActivity(i);

and this is how i receive the intent

 itemList = (List<Item>) getIntent().getParcelableExtra("receiptcart");

this is the full code Cart Code:

public class CreateNewReceiptActivity extends BaseActivity {


    Task<QuerySnapshot> searchitemMerkfilter;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    ItemAdapter adapter;
    List<Item> itemList;
    Item item;
    @BindView(R.id.new_receipt_date)
    TextView date;
    @BindView(R.id.new_receipt_checkstock_itemname)
    EditText etItemType;
    @BindView(R.id.new_receipt_checkstock_itembrand)
    EditText etItemBrand;
    @BindView(R.id.new_receipt_btn_check_stock)
    Button btnCheckStock;
    @BindView(R.id.new_receipt_pb_loading)
    ProgressBar pbLoading;
    @BindView(R.id.new_receipt_tv_tersedia)
    TextView tersedia;
    @BindView(R.id.new_receipt_tv_tidakterdaftar)
    TextView tidakTerdaftar;
    @BindView(R.id.new_receipt_adddetail_itemqty)
    EditText etItemQty;
    @BindView(R.id.new_receipt_adddetail_itemprice)
    EditText etItemPrice;
    @BindView(R.id.new_receipt_adddetail_btn_add_item)
    Button btnAddItem;
    @BindView(R.id.new_receipt_cartitem_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.new_receipt_cartitem_btn_checkout)
    Button btnCheckOut;
    @BindView(R.id.cv_add_detail_item)
    CardView cvDetailItem;
    @BindView(R.id.new_receipt_btn_qr_scan)
    ImageButton qrScanner;
    String itemType, itemBrand, itemid, itemQty, itemPrice, itemDate;
    private IntentIntegrator qrScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_new_receipt);
        etItemType.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
        etItemBrand.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
        itemList = new ArrayList<>();
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ItemAdapter(this, itemList);
        recyclerView.setAdapter(adapter);
        setDate(date);

        qrScan = new IntentIntegrator(this);

                qrScanner.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        qrScan.initiateScan();
                    }
                });

        btnCheckStock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemType = etItemType.getText().toString();
                itemBrand = etItemBrand.getText().toString();
                tersedia.setVisibility(View.GONE);
                tidakTerdaftar.setVisibility(View.GONE);

                if (itemType.isEmpty()) {
                    searchitemMerkfilter = db.collection("watchlist").whereEqualTo("merk", itemType).get();
                    pbLoading.setVisibility(View.VISIBLE);
                    loadItem();
                    // itemList.clear();
                } else if (!itemType.isEmpty()) {
                    itemid = itemType + " - " + itemBrand;
                    searchitemMerkfilter = db.collection("watchlist").whereEqualTo("type", itemid).get();
                    pbLoading.setVisibility(View.VISIBLE);
                    loadItem();
                    // itemList.clear();

                }
            }
        });
        btnAddItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemQty = etItemQty.getText().toString();
                itemPrice = etItemPrice.getText().toString();
                itemDate = date.getText().toString();
                item = new Item("0", itemid, itemQty, itemPrice, itemDate);
                itemList.add(item);
                adapter = new ItemAdapter(CreateNewReceiptActivity.this, itemList);
                recyclerView.setAdapter(adapter);
                cvDetailItem.setVisibility(View.GONE);
                etItemType.setText("");
                etItemBrand.setText("");
                etItemPrice.setText("");
                etItemQty.setText("");
            }
        });
        btnCheckOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pbLoading.setVisibility(View.VISIBLE);
                Intent i = new Intent(CreateNewReceiptActivity.this,ViewFinalReceiptActivity.class);
                i.putExtra("receiptcart", (Parcelable) itemList);
                startActivity(i);
                cutStock();
            }
        });
    }

    private void loadItem() {
        searchitemMerkfilter
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());

                                String id = document.getString("id");
                                String date = document.getString("date");
                                String type = document.getString("type");
                                String qty = document.getString("qty");
                                String price = document.getString("price");
                                item = new Item(id, type, qty, price, date);

                            }
                            // adapter = new ItemAdapter(CreateNewReceiptActivity.this, itemList);
                            //recyclerView.setAdapter(adapter);
                            if (!task.getResult().isEmpty()) {
                                pbLoading.setVisibility(View.GONE);
                                tersedia.setVisibility(View.VISIBLE);
                                cvDetailItem.setVisibility(View.VISIBLE);
                            } else if (task.getResult().isEmpty()) {
                                pbLoading.setVisibility(View.GONE);
                                tidakTerdaftar.setVisibility(View.VISIBLE);
                                cvDetailItem.setVisibility(View.GONE);
                            }
                        } else {
                            Log.w(Tag.ITEM, "error getting documents", task.getException());
                            tidakTerdaftar.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }

and the Receipt View code

public class ViewFinalReceiptActivity extends AppCompatActivity {


    @BindView(R.id.view_final_rc_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.view_final_rc_invoice)
    TextView invoiceNumber;
    @BindView(R.id.view_final_rc_date)
    TextView invoiceDate;
    @BindView(R.id.view_final_rc_btn_print)
    ImageButton btnPrint;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    ItemAdapter adapter;
    Item item;
    List<Item> itemList;
    String type,price,qty,date;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_final_receipt);
        itemList = (List<Item>) getIntent().getParcelableExtra("receiptcart");

       recyclerView.setHasFixedSize(true);
       recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ItemAdapter(this, itemList);
        recyclerView.setAdapter(adapter);
    }
}
  • it's duplicate question you can read this : [Android Class Parcelable with ArrayList](https://stackoverflow.com/questions/22446359/android-class-parcelable-with-arraylist) – Erfan Gholami Jun 26 '19 at 03:12

4 Answers4

0

Use putParcelableArrayList instead to put:

 /**
 * Inserts a List of Parcelable values into the mapping of this Bundle,
 * replacing any existing value for the given key.  Either key or value may
 * be null.
 *
 * @param key a String, or null
 * @param value an ArrayList of Parcelable objects, or null
 */
public void putParcelableArrayList(@Nullable String key,
        @Nullable ArrayList<? extends Parcelable> value) {
    unparcel();
    mMap.put(key, value);
    mFlags &= ~FLAG_HAS_FDS_KNOWN;
}

And getParcelableArrayList for get:

 /**
 * Returns the value associated with the given key, or null if
 * no mapping of the desired type exists for the given key or a null
 * value is explicitly associated with the key.
 *
 * @param key a String, or null
 * @return an ArrayList<T> value, or null
 */
@Nullable
public <T extends Parcelable> ArrayList<T> getParcelableArrayList(@Nullable String key) {
    unparcel();
    Object o = mMap.get(key);
    if (o == null) {
        return null;
    }
    try {
        return (ArrayList<T>) o;
    } catch (ClassCastException e) {
        typeWarning(key, o, "ArrayList", e);
        return null;
    }
}
Edhar Khimich
  • 1,468
  • 1
  • 17
  • 20
0

Try i.putParcelableArrayListExtra("receiptcart", itemList); then when you go to grab it ArrayList<Item> newItems = getIntent().getParcelableArrayListExtra("receiptcart");

Marcus Cantu
  • 463
  • 3
  • 14
  • it runs but in the next activity when i try to get intent. the size is null(no data from the intent). but when i try to debug my putParcelableArrayListExtra there is data/size there – justin junias Jun 26 '19 at 03:18
  • class `Item` must implement `Parcelable`. @justinjunias check my answer please! – TaQuangTu Jun 26 '19 at 03:21
0

I am assuming List< Item > is a list of custom objects of class Item. If that is the case then in your Item class make sure it implements Serializable:

public class Item implements Serializable{

     . . . . . . 

     <autocreate methods required>

}

to pass the list:

myIntent.putExtra("LIST", (Serializable) itemList);
myIntent.startActivity();

to retrieve in other activity:

Intent i = getIntent();
list = (List<Item>) i.getSerializableExtra("LIST");
Haider Malik
  • 1,581
  • 1
  • 20
  • 23
0

Make Item implement Parcelable:

class Item implements Parcelable{}

Send the list by putParcelableArrayListExtra:

intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
startActivity(intent);

Get list from second activity by:

getIntent().getParcelableArrayListExtra("key");
TaQuangTu
  • 2,155
  • 2
  • 16
  • 30