我的目标是生成用户的字符串版本的评论,并通过按钮点击将其添加到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 :(得分: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);
}
});
}
答案 1 :(得分: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);
}
});