2016-11-25 67 views
0

我嘗試使用removeValue()從我的FirebaseDatabase中刪除節點,但它不起作用。奇怪的是,當我在Recyclerview中刪除節點時,節點已成功刪除。無法使用removeValue()從FirebaseDatabase中刪除節點

我使用下面的代碼在新的活動中通過recyclerview的onClick中的ref。在您的按鈕

 final DatabaseReference itemRef = getRef(position); 
      viewHolder.mView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
       // itemRef.removeValue(); 
        Intent intentshowall2one= new Intent(Showcase.this, ShowProduct.class); 
        intentshowall2one.putExtra("S2Skey", itemRef.toString()); 
        Showcase.this.startActivity(intentshowall2one); 

,並在其他活動

key = myIntent.getStringExtra("S2Skey"); 
    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl(key); 
mDatabase.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
     public void onSuccess(Void aVoid) { 
     Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT) 
            .show(); 

      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception exception) { 
       // Uh-oh, an error occurred! 
      Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT) 
       .show(); 
       } 
     }); 

回答

0

感謝您的回答Lutaaya Huzaifah Idris和Vignesh Kumar.I發現這個錯誤是因爲我在我的路徑中使用了希臘文的單詞。一切都很好,上傳下載並用希臘字查詢參考文獻,但是當我傳遞參考文獻給裁判改變的其他活動。這也很奇怪。

0

嘗試了這一點或任何監聽器:

 mDatabase.child(key).removeValue(); 
      Toast.makeText(getApplicationContext(),"The Post has been removed Successfully ",Toast.LENGTH_LONG).show(); 

或使用該

mDatabase.chlid(key).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() { 
     @Override 
      public void onSuccess(Void aVoid) { 
      Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT) 
             .show(); 

       } 
      }).addOnFailureListener(new OnFailureListener() { 
       @Override 
       public void onFailure(@NonNull Exception exception) { 
        // Uh-oh, an error occurred! 
       Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT) 
        .show(); 
        } 
      }); 

相反的:

mDatabase.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
     public void onSuccess(Void aVoid) { 
     Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT) 
            .show(); 

      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception exception) { 
       // Uh-oh, an error occurred! 
      Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT) 
       .show(); 
       } 
     }); 

希望它爲你工作

0

確保您已給予適當引用刪除值之前。那麼,

您可以使用Completion監聽器代替OnSuccessListener,Completion監聽器會給出準確的結果。