我的第一个方法deviceList
可以成功工作,并且我可以在变量String.id
中存储一个值。然后,我尝试在第二个方法addReview1
中使用该字符串以在数据库搜索中实现该字符串。但是,方法addReview1
是通过按钮onclick请求的。我尝试将第二种方法设置为addRview1 (String id)
,但是按钮onclick无效,应用程序将崩溃。因此,在将字符串ID从deviceList
发送到addReview1
时,我需要帮助。
方法deviceList
public void deviceList (View V){
db.collection("devices")
.whereEqualTo("name", value)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for (final QueryDocumentSnapshot document : task.getResult()){
Log.i("DeviceId", document.getId());
String id = document.getId();
}
}
else
{
Log.i("Devices","Error" + task.getException());
Toast.makeText(getBaseContext(),"Error retrieving getting device names",Toast.LENGTH_LONG).show();
}
}
});
};
方法addReview1
public void addReview1 (View V){
Name = (EditText) findViewById(R.id.Name);
Content = (EditText) findViewById(R.id.Content);
Rating = (EditText) findViewById(R.id.Rating);
button4 = (Button) findViewById(R.id.button4);
Map<String, Object> review = new HashMap<>();
review.put("date", new Date());
review.put("name", Name.getText().toString());
review.put("content", Content.getText().toString());
review.put("rating", Integer.parseInt(Rating.getText().toString()));
db.collection("devices").document("1").collection("reviews").add(review)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getApplicationContext(), "Comment added", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
};
答案 0 :(得分:0)
我设法解决了一个简单的问题。
像这样在onCreate上面声明字符串
String NewId;
在deviceList方法中
public void deviceList (View V){
db.collection("devices")
.whereEqualTo("name", value)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for (final QueryDocumentSnapshot document : task.getResult()){
Log.i("DeviceId", document.getId());
NewId = document.getId();
}
}
else
{
Log.i("Devices","Error" + task.getException());
Toast.makeText(getBaseContext(),"Error retrieving getting device names",Toast.LENGTH_LONG).show();
}
}
});
};
在方法addReview1
中public void addReview1 (View V){
Name = (EditText) findViewById(R.id.Name);
Content = (EditText) findViewById(R.id.Content);
Rating = (EditText) findViewById(R.id.Rating);
button4 = (Button) findViewById(R.id.button4);
Map<String, Object> review = new HashMap<>();
review.put("date", new Date());
review.put("name", Name.getText().toString());
review.put("content", Content.getText().toString());
review.put("rating", Integer.parseInt(Rating.getText().toString()));
db.collection("devices").document(NewId).collection("reviews").add(review)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getApplicationContext(), "Comment added", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
};