2017-05-03 81 views
0

我的目標是根據用戶生成字符串版本的評論並將其添加到Firebase中,並點擊按鈕。我只在這個類中寫入數據庫,然後用不同的按鈕從數據庫中讀取數據,並將其帶到另一個類。Android不寫入Firebase數據庫

我翻翻了類似的帖子,並嘗試了這些解決方案,但他們都沒有爲我工作。我改變了規則,因此認證也不是必需的。我們已經附上我的代碼在這裏:

/** 
* create 3 edit texts to be edited by app user 
* 
* @param savedInstanceState 
*/ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fill_out_comment); 

    addCommentButton = (Button) findViewById(R.id.addCommentButton); 
    chooseSongEdit = (EditText) findViewById(R.id.chosenSong); 
    songRatingEdit = (EditText) findViewById(R.id.songRating); 
    songCommentEdit = (EditText) findViewById(R.id.songComment); 

    chooseSongEdit.setText(""); 
    songRatingEdit.setText(""); 
    songCommentEdit.setText(""); 

} 

/** 
* returns song user is commenting on 
* 
* @return string 
*/ 
public String getChosenSong() { 
    EditText chooseSongEdit = (EditText) findViewById(R.id.chosenSong); 
    String chosenSong = chooseSongEdit.getText().toString(); 
    return chosenSong; 
} 

/** 
* returns user song rating 
* 
* @return double 
*/ 
public double getSongRating() { 
    EditText songRatingEdit = (EditText) findViewById(R.id.songRating); 
    String songRatingStr = songRatingEdit.getText().toString(); 
    return Double.parseDouble(songRatingStr); 
} 

/** 
* returns full user comment 
* 
* @return string 
*/ 
public String getSongComment() { 
    EditText songCommentEdit = (EditText) findViewById(R.id.songComment); 
    String songCommentStr = songCommentEdit.getText().toString(); 
    return songCommentStr; 
} 


/** 
* generates full comment; add comment to database 
* @param view 
* @return string 
*/ 
public void generateComment(View view) { 
    //create strings and doubles of user inputs from edit texts 
    String chosenSong = getChosenSong(); 
    Double songRating = getSongRating(); 
    String songComment = getSongComment(); 

    //create StringBuilder that wil contain final comment to be put into setText 
    StringBuilder tempCommentStr = new StringBuilder(); 

    //get user ID to display on comment 
    firebaseAuth = FirebaseAuth.getInstance(); 
    user = firebaseAuth.getCurrentUser(); 
    String uid = "Null User"; 
    if (user != null){ 
     uid = user.getUid(); 
    } 

    tempCommentStr.append("User: " + uid + "\nSong: " + chosenSong + "\nRating: " + 
      songRating + "\nComment: " + songComment); 

    addCommenttoFirebase = tempCommentStr.toString(); 

} 

public void buttonSaveComment(View view) { 
    addCommentButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      database = FirebaseDatabase.getInstance(); 
      ref = database.getReference(); 
      String userID = ref.push().getKey(); 
      ref.push().setValue(userID); 
      generateComment(view); 
      ref.child(userID).setValue(addCommenttoFirebase); 
     } 
    }); 

} 

public void buttonViewAllComments(View view){ 
    Intent intent = new Intent(this, ViewComments.class); 
    startActivity(intent); 
} 

回答

0
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 

public void buttonSaveComment(View view) { 
addCommentButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     String pushID = ref.push().getKey(); 

     CommentObject obj = new CommentObject(pushID, addCommenttoFirebase) 

     ref.child(pushID).setValue(obj); 
    } 
}); 
} 
+0

我試過的值設置爲評論對象,而不是一個字符串,它仍然沒有工作 –

+0

製作確保您的評論obj中的變量名稱和數據庫中的子節點名稱相同。 – Athelon

0

嘗試這個

public void buttonSaveComment(View view) { 
addCommentButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     database = FirebaseDatabase.getInstance(); 
     ref = database.getReference(); 

     String userID = ref.push().getKey(); 

     ref.child(userID).setValue(yourValue).addOnSuccessListener(new OnSuccessListener<Void>() { 
          @Override 
          public void onSuccess(Void aVoid) { 
           //Success 
          } 
         }) 
         .addOnFailureListener(new OnFailureListener() { 
          @Override 
          public void onFailure(@NonNull Exception e) { 
           //Failed 
          } 
         });   
     generateComment(view); 
    } 
});