F
F
fenucss2021-01-10 18:43:28
Java
fenucss, 2021-01-10 18:43:28

Output from firebase to textView?

How to implement output from the database, for example, from reback to the comment line?
5ffb206731081431932336.png

private Button reg_btn;
private EditText usernameInput, phoneInput, rebackInput;
private TextView comment;
private ProgressDialog loadingBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    reg_btn = (Button)findViewById(R.id.reg_btn);
    usernameInput = (EditText)findViewById(R.id.name);
    phoneInput = (EditText)findViewById(R.id.phone);
    rebackInput = (EditText)findViewById(R.id.reback);
    comment = (TextView)findViewById(R.id.comment); 
    loadingBar = new ProgressDialog(this);

    reg_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CreateAccount();
        }
    });
}

private void CreateAccount() {
    String username = usernameInput.getText().toString();
    String phone = phoneInput.getText().toString();
    String reback = rebackInput.getText().toString();

    if (TextUtils.isEmpty(username)) {
        Toast.makeText(this, "Введите имя", Toast.LENGTH_SHORT).show();
    } else
    if (TextUtils.isEmpty(phone)) {
        Toast.makeText(this, "Введите номер", Toast.LENGTH_SHORT).show();
    } else
    if (TextUtils.isEmpty(reback)) {
        Toast.makeText(this, "Введите отзыв", Toast.LENGTH_SHORT).show();
    } else {
        loadingBar.setTitle("Публикация отзыва");
        loadingBar.setMessage("Спасибо за Ваш отзыв!");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();

        ValidatePhone(username, phone, reback);
    }
}

private void ValidatePhone(String username, String phone, String reback) {
    final DatabaseReference RootRef;
    RootRef = FirebaseDatabase.getInstance().getReference();
    RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (!(dataSnapshot.child("user").child(phone).exists())) { // если номер не существует в БД
                HashMap<String, Object> userDataMap = new HashMap<>();
                userDataMap.put("phone", phone);
                userDataMap.put("name", username);
                userDataMap.put("comment", reback);

                RootRef.child("user").child(phone).updateChildren(userDataMap)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            loadingBar.dismiss();
                            Toast.makeText(RegisterActivity.this, "Ваш комментарий успешно добавлен", Toast.LENGTH_SHORT).show();
                        } else {
                            loadingBar.dismiss();
                            Toast.makeText(RegisterActivity.this, "Ошибка создания комментарий, пожалуйста попробуйте снова", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            } else {
                loadingBar.dismiss();
                Toast.makeText(RegisterActivity.this, "С номера: " + phone + " ранее уже был оставлен комментарий" , Toast.LENGTH_SHORT).show(); // если совпал номер в БД, то выдаст сообщение
                Intent loginIntent = new Intent(RegisterActivity.this, RegisterActivity.class);
                startActivity(loginIntent);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question