在TextView的总和列的SQLite Android和显示器

罗伯特·瓦西里:

我有一个SQLite数据库在我的Android项目。我对“车”的活动,在这里我显示来自该数据库的一些项目一RecyclerView。每个项目都有自己的“量”和“价”。我想要做的,是创造,以显示元素的总和的方法(总价格= Elem1(数量*价格)+ elem2时(数量*价格)+ ...)在一个TextView中。我放下我的数据库。我感谢所有帮助!

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "favorites.db";
public static final String TABLE_NAME_CART = "cart_table";
public static final String COL_1 = "IMAGE";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PRICE";
public static final String COL_4 = "DETAILS";
public static final String COL_5 = "MODEL";
public static final String COL_6 = "IMAGEE";
public static final String COL_7 = "IMAGEEE";
public static final String COL_8 = "QUANTITY";
public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

private static DatabaseHelper sDatabaseHelper;

public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}



public static DatabaseHelper getInstance(Context context) {
    if (sDatabaseHelper == null) {
        sDatabaseHelper = new DatabaseHelper(context);
    }
    return sDatabaseHelper;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME_CART + " (" + COL_1 + " INTEGER, " + COL_2 + " VARCHAR, " + COL_3 + " INTEGER, " + COL_4 + " VARCHAR, " + COL_5 + " VARCHAR, " + COL_6 + " INTEGER, " + COL_7 + " INTEGER, " + COL_8 + " INTEGER);");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_CART);
    onCreate(db);
}

// Insert Data in cart table
public boolean insertDataCart(items items) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, items.getlImageId());
    contentValues.put(COL_2, items.getlName());
    contentValues.put(COL_3, items.getlPrice());
    contentValues.put(COL_4, items.getlDetails());
    contentValues.put(COL_5, items.getlModel());
    contentValues.put(COL_6, items.getlImageId1());
    contentValues.put(COL_7, items.getlImageId2());
    contentValues.put(COL_8, items.getQuantity());

    long result = db.insert(TABLE_NAME_CART, null, contentValues);

    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

// See Data Cart
public ArrayList<items> getAllDataCart() {
    ArrayList<items> favItem = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME_CART, null);

    while (res.moveToNext()) {
        int img = res.getInt(0);
        String name = res.getString(1);
        int price = res.getInt(2);
        String details = res.getString(3);
        String model = res.getString(4);
        int img1 = res.getInt(5);
        int img2 = res.getInt(6);
        int quantity = res.getInt(7);
        items newItems = new items(name, price, img, img1, img2, details, model,quantity);
        favItem.add(newItems);
    }
    return favItem;
}

// Sum total items price
public void sumPriceCartItems(int sum) {
    int val;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select "+ COL_3 + "  from " + TABLE_NAME_CART, null);
    Cursor cursor2 = db.rawQuery("select "+ COL_8 + "  from " + TABLE_NAME_CART, null);

    if(cursor.moveToFirst())
    while (cursor.moveToNext() && cursor2.moveToNext()) {
       val = COL_3 * cursor2;
    }

}


// Delete Data Cart
public void deleteCart(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME_CART, COL_1 + "=" + id, null);
}

// Update Cart Quantity
public void updateDataCard(int id, int quantity) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues val = new ContentValues();
    val.put(COL_8, quantity);

    db.update(TABLE_NAME_CART, val, "IMAGE=" + id, null);

}
废去。

更改方法sumPriceCartItems()你的DatabaseHelper这个:

public int sumPriceCartItems() {
    int result = 0;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select sum("+ COL_3 + " * " + COL_8 + ") from " + TABLE_NAME_CART, null);
    if (cursor.moveToFirst()) result = cursor.getInt(0);
    cursor.close();
    db.close(); 
    return result;
}

所以只使用1 Cursor和,而不需要一个循环的返回所需的总和。
在您的活动代码得到的总和,并在显示它TextView

DatabaseHelper db = DatabaseHelper.getInstance(this);
textView.setText("" + db.sumPriceCartItems());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档