我正在制作一个购物应用程序,我想将微调器的选定值上传到我的数据库中。该值正在上载到数据库,但不是正确的值,即正在上载。这是我的代码。
mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.names));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
selectedValue = mySpinner.getSelectedItem().toString();
...
private void addingToCartList()
{
String saveCurrentTime, saveCurrentDate;
Calendar calForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
saveCurrentDate = currentDate.format(calForDate.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calForDate.getTime());
final DatabaseReference cartListRef = FirebaseDatabase.getInstance().getReference().child("Cart List");
final HashMap<String, Object> cartMap = new HashMap<>();
cartMap.put("pid", productID);
cartMap.put("pname", productName.getText().toString());
cartMap.put("price", productPrice.getText().toString());
cartMap.put("date", saveCurrentDate);
cartMap.put("time", saveCurrentTime);
cartMap.put("quantity", selectedValue);
cartMap.put("discount", "");
cartListRef.child("User View").child(Prevelant.currentOnlineUser.getUsername())
.child("Products").child(productID)
.updateChildren(cartMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
cartListRef.child("Admin View").child(Prevelant.currentOnlineUser.getUsername())
.child("Products").child(productID)
.updateChildren(cartMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(ProductDetails2Activity.this, "Added to Cart List", Toast.LENGTH_SHORT).show();
Intent intent = new Intent (ProductDetails2Activity.this, CartActivity.class);
startActivity(intent);
}
}
});
}
}
});
}
每次上传到数据库的值是250
我想解决这个问题...
答案 0 :(得分:0)
我认为您应该在用户单击“提交”之后获得选定的项目,但是您甚至在他进行更改之前就已经获得了该值,因此默认情况下它将采用第一个
private void addingToCartList()
{
String saveCurrentTime, saveCurrentDate;
Calendar calForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
saveCurrentDate = currentDate.format(calForDate.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calForDate.getTime());
//putting spinner here
selectedValue = mySpinner.getSelectedItem().toString();
final DatabaseReference cartListRef = FirebaseDatabase.getInstance().getReference().child("Cart List");
final HashMap<String, Object> cartMap = new HashMap<>();
cartMap.put("pid", productID);
cartMap.put("pname", productName.getText().toString());
cartMap.put("price", productPrice.getText().toString());
cartMap.put("date", saveCurrentDate);
cartMap.put("time", saveCurrentTime);
cartMap.put("quantity", selectedValue);
cartMap.put("discount", "");
cartListRef.child("User View").child(Prevelant.currentOnlineUser.getUsername())
.child("Products").child(productID)
.updateChildren(cartMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
cartListRef.child("Admin View").child(Prevelant.currentOnlineUser.getUsername())
.child("Products").child(productID)
.updateChildren(cartMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(ProductDetails2Activity.this, "Added to Cart List", Toast.LENGTH_SHORT).show();
Intent intent = new Intent (ProductDetails2Activity.this, CartActivity.class);
startActivity(intent);
}
}
});
}
}
});
}
我希望这行得通 Lemme知道它是否有效