Java 如何在从RoomDb获取后添加元素
我在RoomDb中存储了物品的价格。我想获取这些价格并在我的应用程序中汇总,但我无法这样做。这就是我到目前为止所做的。我无法检索价格并对其进行汇总 以下是一个错误:Java 如何在从RoomDb获取后添加元素,java,android,android-room,Java,Android,Android Room,我在RoomDb中存储了物品的价格。我想获取这些价格并在我的应用程序中汇总,但我无法这样做。这就是我到目前为止所做的。我无法检索价格并对其进行汇总 以下是一个错误: Process: com.app.genova, PID: 22586 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a n
Process: com.app.genova, PID: 22586
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.widget.Toast.<init>(Toast.java:132)
at android.widget.Toast.makeText(Toast.java:295)
at android.widget.Toast.makeText(Toast.java:285)
at com.app.genova.CartFragment$1.onChanged(CartFragment.java:79)
at com.app.genova.CartFragment$1.onChanged(CartFragment.java:76)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7081)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:511)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)
CartProduct.java
@Entity(tableName = "MyProduct")
public class CartProduct {
@PrimaryKey(autoGenerate = true)
@NonNull
int id;
@ColumnInfo(name="_pid")
String _pid;
@ColumnInfo(name="name")
String name;
@ColumnInfo(name="price")
int price;
}
@Query("SELECT SUM(price) FROM MyProduct")
LiveData<Integer> getPriceSum();
@Query("SELECT * FROM MyProduct")
List<CartProduct> getAllProducts();
private void loadCart(){
ProductDb db = ProductDb.getInstance(getActivity());
LiveData<Integer> s = db.productDao().getPriceSum();
s.observe(getActivity(), new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
Toast.makeText(getActivity(),"Total "+String .valueOf(integer),Toast.LENGTH_SHORT).show();
}
});
}
ProductDao.java
@Entity(tableName = "MyProduct")
public class CartProduct {
@PrimaryKey(autoGenerate = true)
@NonNull
int id;
@ColumnInfo(name="_pid")
String _pid;
@ColumnInfo(name="name")
String name;
@ColumnInfo(name="price")
int price;
}
@Query("SELECT SUM(price) FROM MyProduct")
LiveData<Integer> getPriceSum();
@Query("SELECT * FROM MyProduct")
List<CartProduct> getAllProducts();
private void loadCart(){
ProductDb db = ProductDb.getInstance(getActivity());
LiveData<Integer> s = db.productDao().getPriceSum();
s.observe(getActivity(), new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
Toast.makeText(getActivity(),"Total "+String .valueOf(integer),Toast.LENGTH_SHORT).show();
}
});
}
@Query(“从MyProduct中选择总和(价格))
LiveData getPriceSum();
@查询(“从MyProduct中选择*)
列出getAllProducts();
CartFragment.java
@Entity(tableName = "MyProduct")
public class CartProduct {
@PrimaryKey(autoGenerate = true)
@NonNull
int id;
@ColumnInfo(name="_pid")
String _pid;
@ColumnInfo(name="name")
String name;
@ColumnInfo(name="price")
int price;
}
@Query("SELECT SUM(price) FROM MyProduct")
LiveData<Integer> getPriceSum();
@Query("SELECT * FROM MyProduct")
List<CartProduct> getAllProducts();
private void loadCart(){
ProductDb db = ProductDb.getInstance(getActivity());
LiveData<Integer> s = db.productDao().getPriceSum();
s.observe(getActivity(), new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
Toast.makeText(getActivity(),"Total "+String .valueOf(integer),Toast.LENGTH_SHORT).show();
}
});
}
private void loadCart(){
ProductDb=ProductDb.getInstance(getActivity());
LiveData s=db.productDao().getPriceSum();
s、 观察(getActivity(),new Observer()){
@凌驾
更改后的公共void(整数){
Toast.makeText(getActivity(),“Total”+String.valueOf(integer),Toast.LENGTH\u SHORT.show();
}
});
}
我做错了什么?总结一下价格,你可以试试下面的方法
int priceSum = 0;
cartList = db.productDao().getAllProducts();
for(CartProduct cartProduct : cartList){
priceSum+=Integer.parseInt(cartProduct.getPrice())
}
//add the corresponding getters in the Entity-class
编辑:
如果您的需求仅仅依赖于
priceSum
,那么如果您将price
更改为整数,您可以通过采用@Zain方法将price
的数据类型更改为int
来避免获取整个列表,您可以使用SQLitesum
函数对Room
本身进行求和
@Query("SELECT SUM(price) FROM MyProduct")
int getPriceSum();
您可以使用LiveData
即时触发价格的任何变化
@Query("SELECT SUM(price) FROM MyProduct")
LiveData<Integer> getPriceSum();
是
cartList
aLiveData
?不,我没有使用livedataSo,您必须调用db.productDao().getAllProducts()代码>在后台线程中。。你是在数据库课上做的吗?。。你能分享它吗?我想知道如何循环使用price并添加它。我也有同样的想法,但是price
的类型是string
我试图得到如下的总和ProductDb=ProductDb.getInstance(context);LiveData s=db.productDao().getPriceSum();Toast.makeText(上下文,s.getValue(),Toast.LENGTH_SHORT.show()代码>。但其显示错误java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int java.lang.Integer.intValue()”
@DigvijayLiveData
在后台线程中工作,因此当您在UI线程中访问它时,它返回null,请检查更新的回答我已经更改了我的实现,正如你现在所做的一样。应用程序没有崩溃,但price sum显示为0,尽管我在数据库中有项目。@Digvijay我已经测试了此代码并正在工作。。可能所有产品的价格都是零。。您能否添加具有价格价值和测试的全新产品