我正在订购应用程序
我正在使用Android studio和firebase数据库,我也在使用sqlite(因为我只是按照youtube上的教程)每当我向我的购物车添加订单时它返回空值
这是我的数据库类
public class Database extends SQLiteAssetHelper{
private static final String DB_NAME="Jerson.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={"MenuId","Name","Quantity","Price"};
String sqlTable="OrderDetails";
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.getString(c.getColumnIndex("MenuId")),
c.getString(c.getColumnIndex("Name")),
c.getInt(c.getColumnIndex("Quantity")),
c.getInt(c.getColumnIndex("Price"))
));
}while (c.moveToNext());
}
return result;
}
public void addToCart(Order order)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO OrderDetails(MenuId,Name,Quantity,Price)VALUES('%s','%s','%s','%s')",
order.getMenuId(),
order.getName(),
order.getQuantity(),
order.getPrice());
db.execSQL(query);
}
public void cleanCart()
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM OrderDetails");
db.execSQL(query);
}
以下是我在CARTclass中调用它的方式
public class Cart extends AppCompatActivity {
List<Order> cart = new ArrayList<>();
CartAdapter adapter;
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
TextView txtTotalPrice;
Button btnPlace;
TextView txtEmail;
private DatabaseReference userData;
private FirebaseUser currentUser;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
database = FirebaseDatabase.getInstance();
DatabaseReference myRef =database.getReference("Food");
requests = database.getReference("Orders");
recyclerView = (RecyclerView) findViewById(R.id.listCart);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
txtTotalPrice = (TextView) findViewById(R.id.total);
btnPlace = (Button) findViewById(R.id.btnPlaceOrder);
txtEmail = (TextView) findViewById(R.id.txtEmail);
btnPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
showAlertDialog();
}
});
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
userData = FirebaseDatabase.getInstance().getReference().child("users").child(currentUser.getUid());
loadListFood();
}
private void showAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("Any Request?");
final EditText editText = new EditText(Cart.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
editText.setLayoutParams(lp);
alertDialog.setView(editText);
alertDialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Orders request = new Orders(
currentUser.getEmail(),
txtTotalPrice.getText().toString(),
editText.getText().toString(),
cart
);
requests.child(String.valueOf(System.currentTimeMillis())).setValue(request);
new Database(getBaseContext()).cleanCart();
Toast.makeText(Cart.this, "Thank you", Toast.LENGTH_LONG).show();
finish();
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
private void loadListFood()
{
cart = new Database(this).getCarts();
adapter = new CartAdapter(cart,this);
recyclerView.setAdapter(adapter);
int total = 0;
for(Order order : cart)
{
total = (order.getQuantity()) * (order.getPrice());
}
txtTotalPrice.setText(String.valueOf(total));
}}
这是订单类
public class Order{
private String MenuId;
private String Name;
private Integer Quantity;
private Integer Price;
public Order(String menuId, String name, Integer quantity, Integer price)
{
MenuId = menuId;
Name = name;
Quantity=quantity;
Price = price;
}
public Order()
{}
public Order(String foodId, String name, String number, Integer price) {
}
public String getMenuId() {
return MenuId;
}
public void setMenuId(String menuId) {
MenuId = menuId;
}
public String getName() {
return Name;
}
public void setName(String name) { Name= name;
}
public Integer getQuantity() {
return Quantity;
}
public void setQuantity(Integer quantity) {
Quantity = quantity;
}
public Integer getPrice() {
return Price;
}
public void setPrice(Integer price) {
Price = price;
}
这是我的适配器和一个类中的viewholder
class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView txt_cart_name,txt_price;
public ImageView img_cart_count;
private ItemClickListener itemClickListener;
public void setTxt_cart_name(TextView txt_cart_name)
{
this.txt_cart_name = txt_cart_name;
}
public CartViewHolder(View itemView)
{
super(itemView);
txt_cart_name =(TextView)itemView.findViewById(R.id.cart_item_name);
txt_price =(TextView)itemView.findViewById(R.id.cart_item_price);
img_cart_count =(ImageView) itemView.findViewById(R.id.cart_item_count);
}
@Override
public void onClick(View v)
{
}}public class CartAdapter extends RecyclerView.Adapter<CartViewHolder>{
private List<Order> listData = new ArrayList<>();
private Context context;
public CartAdapter(List<Order> listData, Context context)
{
this.listData = listData;
this.context = context;
}
@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.cartlayout,parent,false);
return new CartViewHolder(itemView);
}
@Override
public void onBindViewHolder(CartViewHolder holder, int position) {
TextDrawable drawable = TextDrawable.builder()
.buildRound(""+listData.get(position).getQuantity(), Color.RED);
holder.img_cart_count.setImageDrawable(drawable);
int price=(listData.get(position).getPrice())*(listData.get(position).getQuantity());
holder.txt_price.setText(String.valueOf(price));
holder.txt_cart_name.setText(listData.get(position).getName());
}
public int getItemCount() {
return listData.size();
}}
这是我的火炬数据库中的示例
enter image description here 它显示在您可以订购的购物车中,它将转到firebase数据库,但值为null
请帮助谢谢!