-1

I am unable to add this object to my intent:

public void addItem(View view) {
    Item item = new Item();
    item.setName("Test");
    item.setDescription("testDescription");
    item.setQuantity(1.0);
    item.setTaxName("testTexName");
    item.setTaxRate("testTaxRate");
    item.setUnitPrice(5.99);

    ItemList itemList = new ItemList();
    itemList.setItem((List<Item>) item);

    Invoice invoice = new Invoice("due", "0", "USD", "test@mail.com", "testPayer@mail.com",
            "123123", itemList);

    Intent intent = getIntent();
    intent.putExtra("invoice", invoice);
    this.setResult(RESULT_OK, intent);
    finish();

}

Other classes:

public Invoice(String paymentTerms, String discountPercent, String currencyCode, String merchantEmail, String payerEmail, String number, ItemList itemList) {
    this.paymentTerms = paymentTerms;
    this.discountPercent = discountPercent;
    this.currencyCode = currencyCode;
    this.merchantEmail = merchantEmail;
    this.payerEmail = payerEmail;
    this.number = number;
    this.itemList = itemList;
}

public class ItemList {

    @SerializedName("item")
    @Expose
    private List<Item> item = null;

    /**
     * No args constructor for use in serialization
     *
     */
    public ItemList() {
    }

    /**
     *
     * @param item
     */
    public ItemList(List<Item> item) {
        super();
        this.item = item;
    }

    public List<Item> getItem() {
        return item;
    }

    public void setItem(List<Item> item) {
        this.item = item;
    }

    public ItemList withItem(List<Item> item) {
        this.item = item;
        return this;
    }
}

public class Item implements Serializable {

    @SerializedName("name")
    private String _name;
    @SerializedName("description")
    private String _description;
    @SerializedName("quantity")
    private Double _quantity;
    @SerializedName("taxName")
    private String _taxName;
    @SerializedName("taxRate")
    private String _taxRate;
    @SerializedName("unitPrice")
    private Double _unitPrice;

    public Item(String name, String description, Double quantity, String taxRate, String taxName, Double unitPrice) {
        _name = name;
        _description = description;
        _quantity = quantity;
        _taxRate = taxRate;
        _taxName = taxName;
        _unitPrice = unitPrice;
    }

    public Item(String name, String description, Double quantity, Double unitPrice) {
        _name = name;
        _description = description;
        _quantity = quantity;
        _unitPrice = unitPrice;
        _taxName = null;
        _taxRate = null;
    }
}

It is presenting the following error:

cannot resolve method 'putExtra(Java.lang.String, (packagename).Invoice)'.

I am trying to create the following JSON Object:

{
    "paymentTerms": "DueOnReceipt",
    "discountPercent": "0",
    "currencyCode": "USD",
    "number": "1457",
    "payerEmail": "foo@bar.com",
    "itemList": {
        "item": [
            {
                "taxRate": "8.5000",
                "name": "Curtains",
                "description": "Blue curtains",
                "unitPrice": "29.99",
                "taxName": "Tax",
                "quantity": "1"
            },
            {
                "taxRate": "0",
                "name": "Delivery Fee",
                "description": "Delivery Fee",
                "unitPrice": "5.0",
                "taxName": "Tax",
                "quantity": "1"
            }
        ]
    }
}

Can anyone point me in the direction of the correct way of doing this?

Thank you so much for your help, After your suggestions, I have modified the code: Here is the AddItem class:

public void addItem(View view) {

if(validateFields()) {
    Log.d(LOG_TAG, "addItem");


  Intent intent = getIntent();
  intent.putExtra("item", _item);
//  intent.putExtra("item", _item);
  this.setResult(RESULT_OK, intent);
  finish();
  Log.d(LOG_TAG, _item.toString());
} else {
    Toast toast = Toast.makeText(this, "Required field missing!", Toast.LENGTH_SHORT);
    toast.show();
    }
}

public Boolean validateFields() {
    String name = _name.getText().toString();
    String description = _desc.getText().toString();
    String unitPrice = _unitPrice.getText().toString();
    String taxName = _taxName.getText().toString();
    String taxRate = _taxRate.getText().toString();
    String quantity = _quantity.getText().toString();

    if(TextUtils.isEmpty(name) || TextUtils.isEmpty(unitPrice) || TextUtils.isEmpty(quantity)) {
        return false;
    }
    if(TextUtils.isEmpty(taxName) || TextUtils.isEmpty(taxRate)) {
        _item = new Item(name, description, Double.valueOf(quantity), Double.valueOf(unitPrice));
    } else {
        _item = new Item(name, description, Double.valueOf(quantity), taxRate, taxName, Double.valueOf(unitPrice));
    }
    return true;
}

}

Here is the PayPalHereLauncher class, where I receive resultCode=-1 and no data in the intent:

public void addItem(View view) {
    Intent intent = new Intent(this, AddItem.class);
    startActivityForResult(intent, ADD_ITEM_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == ADD_ITEM_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Intent intent = getIntent();
        Item item = (Item)intent.getSerializableExtra("item");

        String name = item.getName();
        String quantity = item.getQuantity().toString();
        Gson gson = new Gson();
        String test = gson.toJson(item);

    }
}

1 Answers1

1

Your Invoice Class must implements class Parcelable or Serializable while you are passing it through an Intent.

See this post to learn how to make a Class Parcelable.

In your onActivityResult :

 Intent intent = getIntent();
 Invoice invoice = (Invoice)intent.getSerializableExtra("invoice");
 List<Item> itemsList = invoice.getItem();
 // Now you can run over a for loop to get all items.
 Item item1 = itemsList.get(0); // First Item . 

Hope this helps !

Anonymous
  • 2,184
  • 15
  • 23
  • Thanks for the help, that did seem to help me add the object to the intent just fine. I debugged and confirmed that the intent has mExtras with the object present. After finish() and going back to calling Activity, I still can't access the data. I am getting resultCode = -1 and the intent is empty. Any tips? – willpittman432 Apr 15 '18 at 03:13
  • @willpittman432 : Can you post your onActivityResult , so that I can debug at my end – Anonymous Apr 15 '18 at 09:40
  • Thank you @Shubham Srivastava, I really appreciate the help. – willpittman432 Apr 15 '18 at 18:54
  • @Subham Srivastava, I modified the code above. Thanks again! – willpittman432 Apr 15 '18 at 19:09
  • Okay , will check the same ! – Anonymous Apr 15 '18 at 19:14
  • thanks for the update. I'm still getting resultCode=-1 in onActivityResult(). I confirmed again that the intent in AddItem had mExtras, and then in onActivityResult(), I got resultCode=-1 – willpittman432 Apr 15 '18 at 20:52
  • Check value for Activity.RESULT_OK . It is = -1 , so that will pass . You have sent the right resultCode and receiving the same appropriately – Anonymous Apr 15 '18 at 21:15
  • Thank you @Shubham Srivastava I got it working finally, I appreciate all the help – willpittman432 Apr 17 '18 at 01:20