2017-05-03 83 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); 
    } 
});