我真的需要帮助,而每当我按下购物车按钮添加食物到篮子应用程序停止工作,并且当购物车按钮不起作用时无法提交我的作业。我使用Firebase正在运行,数据库浏览器用于SQLite。我三倍 - 用代码中的名字对数据库中的名称进行四次搜索,但我仍然不知道为什么它不起作用。非常感谢任何帮助。
图片错误(LogCat):https://ibb.co/n4bMWb
数据库代码。
package com.example.onix.AndroEat.Database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import java.util.ArrayList;
import java.util.List;
import com.example.onix.AndroEat.Model.Order;
public class Database extends SQLiteAssetHelper {
private static final String DB_NAME="Eat.db";
private static final int DB_VER=1;
public Database(Context context) {
super(context, DB_NAME,null, DB_VER);
}
public List<Order> getCarts()
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect={"ID","ProductName","ProductId","Quantity","Price",};
String sqlTable="OrderDetail";
qb.setTables(sqlTable);
Cursor c = qb.query(db,sqlSelect,null,null,null,null,null);
final List<Order> result = new ArrayList<>();
if(c.moveToFirst())
{
do{
result.add(new Order(
c.getInt(c.getColumnIndex("ID")),
c.getString(c.getColumnIndex("ProductId")),
c.getString(c.getColumnIndex("ProductName")),
c.getString(c.getColumnIndex("Quantity")),
c.getString(c.getColumnIndex("Price"))
));
}while (c.moveToNext());
}
return result;
}
public void addToCart(Order order) {
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,)VALUES('%s','%s','%s','%s');",
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice());
db.execSQL(query);
}
public void cleanCart()
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM OrderDetail");
db.execSQL(query);
}
public int getCountCart() {
int count=0;
SQLiteDatabase db = getReadableDatabase();
String query = String.format("SELECT COUNT(*) FROM OrderDetail");
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst())
{
do{
count = cursor.getInt(0);
}while (cursor.moveToNext());
}
return count;
}
public void updateCart(Order order) {
SQLiteDatabase db = getReadableDatabase();
String query = String.format("UPDATE OrderDetail SET Quantity= %s WHERE ID = %d",order.getQuantity(),order.getID());
db.execSQL(query);
}
}
&#13;
FoodDetail代码:
package com.example.onix.AndroEat;
import android.content.Context;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.andremion.counterfab.CounterFab;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.example.onix.AndroEat.Common.Common;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import com.stepstone.apprating.AppRatingDialog;
import com.stepstone.apprating.listener.RatingDialogListener;
import java.util.Arrays;
import com.example.onix.AndroEat.Model.Food;
import com.example.onix.AndroEat.Model.Order;
import com.example.onix.AndroEat.Model.Rating;
import com.example.onix.AndroEat.Database.Database;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
public class FoodDetail extends AppCompatActivity implements RatingDialogListener {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnRating;
CounterFab btnCart;
ElegantNumberButton numberButton;
RatingBar ratingBar;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
DatabaseReference ratingTbl;
Food currentFood;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/restaurant_font.otf")
.setFontAttrId(R.attr.fontPath)
.build());
setContentView(R.layout.activity_food_detail);
//Firebase
database = FirebaseDatabase.getInstance();
foods = database.getReference("Foods");
ratingTbl = database.getReference("Rating");
//Init view
numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
btnCart = (CounterFab) findViewById(R.id.btnCart);
btnRating = (FloatingActionButton)findViewById(R.id.btn_rating);
ratingBar = (RatingBar)findViewById(R.id.ratingBar);
btnRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRatingDialog();
}
});
btnCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
currentFood.getName(),
numberButton.getNumber(),
currentFood.getPrice()
));
Toast.makeText(FoodDetail.this, "Added to Cart", Toast.LENGTH_SHORT).show();
}
});
btnCart.setCount(new Database(this).getCountCart());
food_description = (TextView)findViewById(R.id.food_description);
food_name = (TextView)findViewById(R.id.food_name);
food_price = (TextView)findViewById(R.id.food_price);
food_image = (ImageView)findViewById(R.id.img_food);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
//Get Food Id from Intent
if(getIntent() != null)
foodId = getIntent().getStringExtra("FoodId");
if(!foodId.isEmpty())
{
if(Common.isConnectedToInterner(getBaseContext()))
{
getDetailFood(foodId);
getRatingFood(foodId);
}
else
{
Toast.makeText(FoodDetail.this, "Please check your connection !!", Toast.LENGTH_SHORT).show();
return;
}
}
}
private void getRatingFood(String foodId) {
com.google.firebase.database.Query foodRating = ratingTbl.orderByChild("foodId").equalTo(foodId);
foodRating.addValueEventListener(new ValueEventListener() {
int count=0,sum=0;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Rating item = postSnapshot.getValue(Rating.class);
sum+=Integer.parseInt(item.getRateValue());
count++;
}
if(count != 0)
{
float average = sum/count;
ratingBar.setRating(average);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showRatingDialog() {
new AppRatingDialog.Builder()
.setPositiveButtonText("Submit")
.setNegativeButtonText("Cancel")
.setNoteDescriptions(Arrays.asList("Very Bad","Not Good","Quite Ok","Very Good","Excellent"))
.setDefaultRating(1)
.setTitle("Rate this food")
.setDescription("Please select some stars and give your feedback")
.setTitleTextColor(R.color.colorPrimary)
.setDescriptionTextColor(R.color.colorPrimary)
.setHint("Please write your comment here...")
.setHintTextColor(R.color.colorAccent)
.setCommentTextColor(android.R.color.white)
.setCommentBackgroundColor(R.color.colorPrimaryDark)
.setWindowAnimation(R.style.RatingDialogFadeAnim)
.create(FoodDetail.this)
.show();
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentFood = dataSnapshot.getValue(Food.class);
//Set Image
Picasso.with(getBaseContext()).load(currentFood.getImage())
.into(food_image);
collapsingToolbarLayout.setTitle(currentFood.getName());
food_price.setText(currentFood.getPrice());
food_name.setText(currentFood.getName());
food_description.setText(currentFood.getDescription());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onPositiveButtonClicked(int value, String comments) {
//Get Rating and upload to firebase
final Rating rating = new Rating(Common.currentUser.getPhone(),
foodId,
String.valueOf(value),
comments);
ratingTbl.child(Common.currentUser.getPhone()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(Common.currentUser.getPhone()).exists())
{
//Remove old value (you can delete or let it be - useless function :D)
ratingTbl.child(Common.currentUser.getPhone()).removeValue();
//Update new value
ratingTbl.child(Common.currentUser.getPhone()).setValue(rating);
}
else
{
//Update new value
ratingTbl.child(Common.currentUser.getPhone()).setValue(rating);
}
Toast.makeText(FoodDetail.this, "Thank you for submit rating !!!", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onNegativeButtonClicked() {
}
}
&#13;
如果需要,会发布更多代码。
非常感谢。
答案 0 :(得分:0)
&#34;&#34;应在上述代码中的价格后删除。以下是仪式代码
www-data ALL=(ALL) NOPASSWD: /var/www/html/config.sh
同时删除&#34;,&#34;从上面的代码
String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price)VALUES('%s','%s','%s','%s');",
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice());