I am doing this guide link and i cant call setValueAsync i get error Cannot resolve method 'setValueAsync' in 'DatabaseReference'. I saw that I can use it at the start where I set String but I don't know how can it help me. Thanks all.
My code:
public class MessageMembersList extends Fragment implements View.OnClickListener {
private Button btnConnectToServer;
FirebaseAuth mAuth;
FirebaseUser user;
String curentName;
String curentLastName;
String curentEmail;
String Name;
String LastName;
String Email;
public Users(String Name, String LastName, String Email) {
this.Name = Name;
this.LastName = LastName;
this.Email = Email;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
btnConnectToServer = (Button) getView().findViewById(R.id.btnConnectToServer);
mAuth = FirebaseAuth.getInstance();
btnConnectToServer.setOnClickListener(this);
user = mAuth.getCurrentUser();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference().child("No server").child(user.getUid());
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot datasnapshot) {
curentName = (String) datasnapshot.child("name").getValue().toString();
curentLastName = (String) datasnapshot.child("lastName").getValue().toString();
curentEmail = (String) datasnapshot.child("email").getValue().toString();
txt1.setText(Name);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
Error part setValueAsync (Cannot resolve method 'setValueAsync' in 'DatabaseReference'). Users have an error to but i am not gona talk about it in this question.
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnConnectToServer:
connectedToServer = false;
WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
serverName = ssid;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference().child(ssid);
myRef.child(user.getUid()).setValueAsync(new Users(curentName,curentLastName,curentEmail));
break;
}
}
CodePudding user response:
The setValueAsync()
method is a part of the Firebase Admin SDK and does not work in Android. If you need to add a value to the Realtime Database in an Android fragment, you have to use the Fireabse Android SDK and use setValue()
method as explained in the official documentation for writing data.