我正在制作一个应用程序,其中有一个名为“Full tank”的复选框。如果我选中此框,我必须计算价值里程并将其保存到数据库中。如果未选中该框,则不计算里程,并将 0 存储在里程列中。但是,当我第一次进入我的数据库时,我希望里程为 0,而不管复选框是否被选中。
任何人都可以帮我解决这个想法吗?这是我到目前为止尝试过的东西,但是这段代码也会生成第一行的里程数:-
public void calculateMileage(FuelExpense fe)
{
// TODO Auto-generated method stub
String [] columns = new String[]{KEY_ROW_ID, KEY_KM, KEY_FUEL_QTY, KEY_FUEL_PRICE, KEY_TOTAL_COST, KEY_MILEAGE, KEY_DATE,KEY_TANK_FULL};
long numRows = DatabaseUtils.queryNumEntries(ourDatabase, "fuel_table");
if(fe.getTankFullstatus()==true)
{
mileageCursor = ourDatabase.rawQuery("SELECT * FROM fuel_table ORDER BY _id DESC LIMIT 1", null);
if(mileageCursor!=null && mileageCursor.getCount()>0)
{
mileageCursor.moveToLast();
String lastKm = mileageCursor.getString(1);
String lastFuelQty = mileageCursor.getString(2);
lastDBFuelEntry.setKm(Long.parseLong(lastKm));
lastDBFuelEntry.setFuelQty(Double.parseDouble(lastFuelQty));
}
else
{
mileage = 0.0d;
}
}
else
{
for(mileageCursor.moveToLast();!mileageCursor.isBeforeFirst();mileageCursor.moveToPrevious())
{
nftQty = nftQty+lastDBFuelEntry.getFuelQty();
}
}
fuelUsed = nftQty + fe.getFuelQty();
mileage =(fe.getKm() - lastDBFuelEntry.getKm())/ fuelUsed;
mileage= (double)(Math.round(mileage*100))/100;
fe.setMileage(mileage);
}
Table creation:-
//Fuel info table
db.execSQL( "CREATE TABLE "+FUEL_DATABASE_TABLE +" (" +
KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_KM + " LONG NOT NULL, "+
KEY_FUEL_QTY + " INTEGER NOT NULL, "+
KEY_FUEL_PRICE + " DOUBLE NOT NULL, "+
KEY_TOTAL_COST + " DOUBLE NOT NULL, "+
KEY_MILEAGE + " DOUBLE DEFAULT 0, "+
KEY_DATE + " DATE NOT NULL, "+
KEY_TANK_FULL + " TEXT);"
);
Insert:-
{
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_KM, fe.getKm());
cv.put(KEY_FUEL_QTY, fe.getFuelQty());
cv.put(KEY_FUEL_PRICE,fe.getFuelPrice());
cv.put(KEY_TOTAL_COST, fe.getTotalCost());
cv.put(KEY_MILEAGE, fe.getMileage());
cv.put(KEY_DATE, fe.getFuelFilledDate());
cv.put(KEY_TANK_FULL, fe.getTankFullstatus());
return ourDatabase.insert(FUEL_DATABASE_TABLE, null, cv);
}