协助处理
KF300066
// 修正后代码
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
public class ReportActivity extends AppCompatActivity {
private DatabaseReference databaseRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
// 初始化Firebase数据库
databaseRef = FirebaseDatabase.getInstance().getReference("reports");
// 获取报告按钮的引用
findViewById(R.id.report_button).setOnClickListener(v -> reportContent());
}
private void reportContent() {
String content = getEditTextValue(R.id.content_edit_text);
String reason = getEditTextValue(R.id.reason_edit_text);
if (content == null || reason == null) {
Toast.makeText(this, "Please enter both content and reason", Toast.LENGTH_SHORT).show();
return;
}
// 使用Firebase数据库上传举报数据
HashMap<String, Object> reportData = new HashMap<>();
reportData.put("content", content);
reportData.put("reason", reason);
databaseRef.push().setValue(reportData)
.addOnSuccessListener(documentReference -> {
Toast.makeText(this, "Report submitted successfully!", Toast.LENGTH_SHORT).show();
})
.addOnFailureListener(e -> {
Toast.makeText(this, "Failed to submit report: " + e.getMessage(), Toast.LENGTH_SHORT).show();
});
}
private String getEditTextValue(int id) {
EditText editText = findViewById(id);
return editText.getText().toString().trim();
}
主要修正点:1、在reportContent方法中添加了对输入字段是否为空的检查。2、使用getEditTextValue方法简化了获取EditText值的操作。3、在reportContent方法中使用HashMap代替databaseRef.push().setValue(reportData),这样可以更灵活地管理报告数据。协助处理
KF300066