我有这样一个类:
public class DeliveryItem {
private int _id;
private String _invoiceNumber;
private String _UPC_PLU;
private String _vendorItemId;
private int _packSize;
private String _description;
private BigDecimal _cost;
private BigDecimal _margin;
private BigDecimal _listPrice;
private int _departmentNumber;
private String _subdepartment;
private String _quantity;
public void setID(int id) {
this._id = id;
}
public int getID() {
return this._id;
}
. . .
成本,利润率和listprice列“的BigDecimal”因为原单(C#)应用程序,他们十进制
相应的SQLite表这样定义:
String CREATE_DELIVERYITEMS_TABLE = "CREATE TABLE " +
TABLE_DELIVERYITEMS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_INVOICENUM + " TEXT,"
+ COLUMN_UPCPLU + " TEXT," + COLUMN_VENDORITEMID + " TEXT,"
+ COLUMN_PACKSIZE + " INTEGER," + COLUMN_DESCRIPTION + " TEXT," + COLUMN_COST +
" REAL,"
+ COLUMN_MARGIN + " REAL," + COLUMN_LISTPRICE + " REAL," + COLUMN_DEPTNUM + "
INTEGER,"
+ COLUMN_SUBDEPT + " TEXT," + COLUMN_QTY + " TEXT"
+ ")";
最近SQLite的获取到小数点(C#)或BigDecimal的(JAVA)是真实的,SQLite的唯一的浮点数据类型。
在扩展SQLiteOpenHelper类,我试图从表中选择值,并将其存储在我的自定义类的类型,像这样一个ArrayList:
public ArrayList<DeliveryItem> getAllDeliveryItems() {
ArrayList<DeliveryItem> delItems = new ArrayList<DeliveryItem>();
String query = "Select * FROM " + TABLE_DELIVERYITEMS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db != null ? db.rawQuery(query, null) : null;
if (cursor != null) {
while (cursor.moveToNext()) {
DeliveryItem d = new DeliveryItem();
d.setID(Integer.parseInt(cursor.getString(0)));
. . .
d.set_cost(BigDecimal.valueOf(cursor.getString(6)));
d.set_margin(BigDecimal.valueOf(cursor.getString(7)));
d.set_listPrice(BigDecimal.valueOf(cursor.getString(8)));
. . .
delItems.add(d);
}
}
if (cursor != null) {
cursor.close();
}
return delItems;
}
请注意,我可以用“cursor.getString(N)”,并使用整数瓦尔斯分配串瓦尔斯到DeliveryItem类“ 的Integer.parseInt(cursor.getString(0))”,但作为对的BigDecimal值,我遇到一个问题。
Java的BigDecimal的数据类型/类没有相关的“parseBigDecimal”的方法。
如果我试试这个:
d.set_cost(BigDecimal.valueOf(cursor.getString(6)));
我得到的,“ 错误:找到的valueOf没有合适的方法(String)方法BigDecimal.valueOf(长)是不适用(自变量不匹配;串不能被转换为长)方法BigDecimal.valueOf(双)是不适用(自变量不匹配;字符串不能转换为double)“
...如果我试试这个:
d.set_cost(Float.parseFloat(cursor.getString(6)));
......我得到的,“ 错误:不兼容的类型:浮动不能转换为BigDecimal”
如果我尝试Double.parseDouble(),我得到了类似“......双不能转换为BigDecimal”
我怎样才能从数据库中读取实数和填充这些丘壑BigDecimal在瓦尔斯?
UPDATE
我甚至现在能“假,直到我让”这个临时代码:
d.set_cost(42.00);
...或这个:
d.set_cost((BigDecimal)42.00);
...因为他们两个用,抱怨“错误:不兼容的类型:双不能转换为BigDecimal”
而且同样适用于这种替代铸造语法, Droidio建议:
d.set_cost(((BigDecimal) 42.00));