我正在尝试提供数据库参考,以将一些数据上传到我的Firebase实时数据库。我已经在数据库中有一些数据,并且正在检索它。我有一个微调器,用户可以在其中选择要向其发送消息的特定用户。因此,我想将数据上传到所选用户的UID中。
我尝试直接检索所选用户的UID,但是它不起作用,因此我在注册过程中创建了一个名为UID的孩子。现在,我可以检索所选用户的UID,并将其存储在字符串'key'中。我想将“ key”用作数据库引用中的子引用,以便可以在所选用户下上传数据。我已经检查了'key'的值,它是正确的UID,即所选用户的UID,但是当我在数据库引用中使用此String时,我的应用程序崩溃。
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
NameSelected = item.toString();
pris = NameSelected;
ref = FirebaseDatabase.getInstance().getReference().child("Faculty_Database");
ref.orderByChild("Name").equalTo(pris).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
key = datas.child("UID").getValue(String.class);
}
}
//
SubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (NameEditText.getText().toString().equals("") || PhoneNumberEditText.getText().toString().equals("") || CompanyEditText.getText().toString().equals("") || EmailEditText.getText().toString().equals("") || AppointmentEditText.getText().toString().equals("") || image.equals(null)) {
Toast.makeText(getApplicationContext(), "Please fill all details", Toast.LENGTH_LONG).show();
} else {
final StudentDetails studentDetails = new StudentDetails();
GetDataFromEditText();
studentDetails.setName(NameHolder);
studentDetails.setPhoneNumber(NumberHolder);
studentDetails.setemail(EmailHolder);
studentDetails.setCompanyName(CompanyHolder);
studentDetails.setAppointment(AppointmentHolder);
studentDetails.setNameSelected(NameSelected);
// Getting the ID from firebase database.
final String StudentRecordIDFromServer = databaseReference.push().getKey();
filePath = FirebaseStorage.getInstance().getReference().child("Visitors");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] dataToUpload = baos.toByteArray();
UploadTask uploadTask = filePath.putBytes(dataToUpload);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
downloadUri= task.getResult();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Faculty_Database").child(key).child("Received");
Map<String, Object> mapToUpload = new HashMap<>();
mapToUpload.put("imageUrl", downloadUri.toString());
mapToUpload.put("Data", studentDetails);
databaseReference.child(StudentRecordIDFromServer).setValue(mapToUpload);
} else {
// Handle failures
// ...
}
}
});
}
// Showing Toast message after successfully data submit.
Toast.makeText(SecurityActivity.this, "Request Sent Successfully!", Toast.LENGTH_LONG).show();
}
});
这是我的数据库
请告诉我如何在所选用户的UID下创建一个名为“已接收”的子代来上传我的数据?
答案 0 :(得分:0)
添加用于在onDataChange
方法内设置数据的代码,因为onDataChange
是异步的,这意味着在获取key
之前将执行设置数据的代码。 >
ref.orderByChild("Name").equalTo(pris).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
key = datas.child("UID").getValue(String.class);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Faculty_Database").child(key).child("Received");
Map<String, Object> mapToUpload = new HashMap<>();
mapToUpload.put("imageUrl", downloadUri.toString());
mapToUpload.put("Data", studentDetails);
databaseReference.child(StudentRecordIDFromServer).setValue(mapToUpload);
}
}