Answer the question
In order to leave comments, you need to log in
Firebase how to display full name in textview?
registered users of the database
need to display the last name, first name and patronymic in the textview, how to do this? below I throw off all the code that I have, please write only if you can help, and not give advice on what to read, how much I don’t read I can’t understand how to draw a conclusion, I need a short specific example to figure it out, but I didn’t find it anywhere on the Internet.
nav_header_mapi.xml
MapiActivity.java
public class MapiActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private FirebaseDatabase database;
private DatabaseReference reference;
TextView fio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapi);
database = FirebaseDatabase.getInstance();
reference = database.getReference("users");
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
private void updateList(){
reference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mapi, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Answer the question
In order to leave comments, you need to log in
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnSignIn, btnRegister;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
RelativeLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = findViewById(R.id.btnSignIn);
btnRegister = findViewById(R.id.btnRegister);
root = findViewById(R.id.root_element);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("users");
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRegisterWindoW();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSignInWindow();
}
});
}
private void showSignInWindow() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Войти");
dialog.setMessage("Введите данные для входа");
LayoutInflater inflater = LayoutInflater.from(this);
View sign_in_window = inflater.inflate(R.layout.sign_in_window, null);
dialog.setView(sign_in_window);
final MaterialEditText email = sign_in_window.findViewById(R.id.emailField);
final MaterialEditText pass = sign_in_window.findViewById(R.id.passField);
dialog.setNegativeButton("Отменить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
}
});
dialog.setPositiveButton("Войти", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (TextUtils.isEmpty(email.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (pass.getText().toString().length() < 5) {
Snackbar.make(root, "Пароль должен быть более 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
auth.signInWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(MainActivity.this, MapiActivity.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(root, "Ошибка авторизации" + e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
});
}
});
dialog.show();
}
private void showRegisterWindoW() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Зарегистрироваться");
dialog.setMessage("Введите данные для регистрации");
LayoutInflater inflater = LayoutInflater.from(this);
View register_window = inflater.inflate(R.layout.register_window, null);
dialog.setView(register_window);
final MaterialEditText email = register_window.findViewById(R.id.emailField);
final MaterialEditText pass = register_window.findViewById(R.id.passField);
final MaterialEditText name = register_window.findViewById(R.id.nameField);
final MaterialEditText mname = register_window.findViewById(R.id.mnameField);
final MaterialEditText lname = register_window.findViewById(R.id.lnameField);
final MaterialEditText klass = register_window.findViewById(R.id.klassField);
dialog.setNegativeButton("Отменить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
}
});
dialog.setPositiveButton("Добавить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (TextUtils.isEmpty(email.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (pass.getText().toString().length() < 5) {
Snackbar.make(root, "Пароль должен быть более 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(name.getText().toString())) {
Snackbar.make(root, "Введите ваше имя", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(mname.getText().toString())) {
Snackbar.make(root, "Введите вашу фамилию", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(lname.getText().toString())) {
Snackbar.make(root, "Введите ваше отчество", Snackbar.LENGTH_SHORT).show();
return;
}
//регистрация пользователя
auth.createUserWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
User user = new User();
user.setEmail(email.getText().toString());
user.setName(name.getText().toString());
user.setMname(mname.getText().toString());
user.setLname(lname.getText().toString());
user.setPass(pass.getText().toString());
user.setKlass(klass.getText().toString());
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(root, "Пользователь добавлен", Snackbar.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(root, "Ошибка регистрации." + e.getMessage(),Snackbar.LENGTH_LONG).show();
}
});
}
});
dialog.show();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question