如何防止将空数据保存到Firebase数据库?

时间:2017-12-20 07:20:39

标签: android firebase firebase-realtime-database

enter image description here

我正在尝试阻止我的应用保存空数据。例如,我忘了键入条形码编号,空白数据不会被发送到firebase。目前,如果我忘记键入条形码编号条形码编号,空白数据将保存到我的数据库中,这是我不想要的。

这是我的代码:

b2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            String B=et7.getText().toString();
            String PN=et8.getText().toString();
            String E=et9.getText().toString();
            String D=et10.getText().toString();
            String Q=et11.getText().toString();
            String name=et20.getText().toString();
            String number=et21.getText().toString();
            String email=et22.getText().toString();
            Product PO=new Product(B,PN,E,D,Q,name,number,email);
           // reference.child(B).setValue(PO);
            reference.push().setValue(PO);
            Toast.makeText(getApplicationContext(),"Data Successfully Saved",Toast.LENGTH_LONG).show();
            startActivity(new Intent(AddProduct.this, Inventory.class));
        }

2 个答案:

答案 0 :(得分:2)

在向firebase发送数据之前,请检查所有字段是否为空。

b2.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        String B=et7.getText().toString();
        String PN=et8.getText().toString();
        String E=et9.getText().toString();
        String D=et10.getText().toString();
        String Q=et11.getText().toString();
        String name=et20.getText().toString();
        String number=et21.getText().toString();
        String email=et22.getText().toString();
        if(B.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else if(PN.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else if(EB.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else if(DQ.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else if(.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else if(number.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else if(email.isEmpty()){
           Toast.makeText(getApplicationContext(),"Field Name is mandatory",Toast.LENGTH_LONG).show(); 
        }else{
        Product PO=new Product(B,PN,E,D,Q,name,number,email);
       // reference.child(B).setValue(PO);
        reference.push().setValue(PO);
        Toast.makeText(getApplicationContext(),"Data Successfully Saved",Toast.LENGTH_LONG).show();
        startActivity(new Intent(AddProduct.this, Inventory.class));
      }
    }

我认为你在寻找这个。

答案 1 :(得分:1)

试试这个:

声明变量后,在onClick内部执行此操作:

 String B=et7.getText().toString();
if(B.isEmpty()){
     Toast.makeText(getApplicationContext(),"It is empty",Toast.LENGTH_LONG).show();
 return;
 }

这样,如果数据库中为空,则不会保存。

或者你可以这样做:

if(B.isEmpty()){
 //don't save
   }
  else{
    reference.child(B).setValue(PO);
   }