У меня возникла фатальная проблема с Firebase во время работы над приложением доставки еды. Когда я даю значение для адреса в диалоговом окне с предупреждением, я вижу на своей базе Firebase, что оно хранится в поле общего количества. Просто не могу понять, как это решить.
Вот мой Request.java
Файл
public class Request{
private String phone;
private String name;
private String address;
private String total;
private List<Order>foods;
public Request() {
}
public Request(String phone, String name, String address, String total, List<Order> foods) {
this.phone = phone;
this.name = name;
this.address = address;
this.total = total;
this.foods = foods;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public List<Order> getFoods() {
return foods;
}
public void setFoods(List<Order> foods) {
this.foods = foods;
}
}
Вот мой Cart.java
файл:
public class Cart extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
TextView txtTotalPrice;
Button btnPlace;
List<Order> cart=new ArrayList<>();
CartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
//Firebase
database=FirebaseDatabase.getInstance();
requests=database.getReference("Requests");
//Init
recyclerView=findViewById(R.id.listCart);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
txtTotalPrice=findViewById(R.id.total);
btnPlace=findViewById(R.id.btnPlaceOrder);
btnPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});
LoadListFood();
}
private void showAlertDialog() {
AlertDialog.Builder alertDialog=new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("One more step!");
alertDialog.setMessage("Enter your address :");
final EditText edtAddress=new EditText(Cart.this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
edtAddress.setLayoutParams(lp);
alertDialog.setView(edtAddress); //Add edit text to alert dialog
alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Create new request
Request request=new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
txtTotalPrice.getText().toString(),
edtAddress.getText().toString(),
cart
);
//Submit to Firebase
//We will using System.currentMili to key
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Delete cart
new Database(getBaseContext()).cleanCart();
Toast.makeText(Cart.this, "Thank you. Order placed", Toast.LENGTH_SHORT).show();
finish();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void LoadListFood() {
cart=new Database(this).getCarts();
adapter=new CartAdapter(cart,this);
recyclerView.setAdapter(adapter);
//Calculate total price
int total=0;
for (Order order:cart)
total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));
Locale locale=new Locale("en","US");
NumberFormat fmt=NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
}
}
Вот мой FoodDetail.java
:
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
Food currentfood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//Firebase
database=FirebaseDatabase.getInstance();
foods=database.getReference("Foods");
//Init View
numberButton=findViewById(R.id.number_button);
btnCart=findViewById(R.id.btnCart);
btnCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
currentfood.getName(),
numberButton.getNumber(),
currentfood.getPrice(),
currentfood.getDiscountmenuId()
));
Toast.makeText(FoodDetail.this, "Added to cart", Toast.LENGTH_SHORT).show();
}
});
food_description=findViewById(R.id.food_description);
food_name=findViewById(R.id.food_name);
food_price=findViewById(R.id.food_price);
food_image=findViewById(R.id.img_food);
collapsingToolbarLayout=findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapseAppbar);
//get Food Id from intent
if (getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if (foodId != null && !foodId.isEmpty()){
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
currentfood=dataSnapshot.getValue(Food.class);
//set Image
Picasso.get().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(@NonNull DatabaseError databaseError) {
}
});
}
}
При вводе адреса это выглядит так:
![](https://i.stack.imgur.com/7uIYS.png)