我无法将此对象添加到我的意图中:
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();
}
其他课程:
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;
}
}
它出现以下错误:
无法解析方法&#39; putExtra(Java.lang.String,(packagename).Invoice)&#39;。
我正在尝试创建以下JSON对象:
{
"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"
}
]
}
}
有人能指出我正确的方法吗?
非常感谢你的帮助,经过你的建议,我修改了代码: 这是AddItem类:
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;
}
}
这是PayPalHereLauncher类,我收到resultCode = -1且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);
}
}
答案 0 :(得分:1)
当您通过Invoice
时,Intent
班级必须实施班级Parcelable或Serializable 。
请参阅this post了解如何制作课程Parcelable
。
在你的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 .
希望这有帮助!