求和列 SQLite Android 并在 TextView 上显示 [关闭]
Posted
技术标签:
【中文标题】求和列 SQLite Android 并在 TextView 上显示 [关闭]【英文标题】:Sum column SQLite Android and display on TextView 【发布时间】:2020-06-09 07:24:31 【问题描述】:我的 android 项目中有一个 SQLite 数据库。我有一个用于“购物车”活动的 RecyclerView,我在其中显示了该数据库中的一些项目。每件商品都有自己的“数量”和“价格”。我想做的是创建一种方法来在一个TextView中显示元素的总和(总价格= Elem1(数量*价格)+ Elem2(数量*价格)+ ...)。我放下了我的数据库。感谢您的帮助!
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);
【问题讨论】:
【参考方案1】:将DatabaseHelper
的方法sumPriceCartItems()
更改为:
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());
【讨论】:
以上是关于求和列 SQLite Android 并在 TextView 上显示 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
将每小时数据重新采样为每日数据并在 python 中的特定列上求和
Android 日记应用 - 搜索 SQLite 数据库并在 ListView 中显示结果
我有从 sqlite 检索数据并在 BaseAdapter 中显示数据的 android 应用程序 .. 最好的方法是啥?