[英]Data from the instagram clone android application are not reflecting at the realtime database in Firebase
I am trying to build a clone of Instagram application in Android Studio.我正在尝试在 Android Studio 中构建 Instagram 应用程序的克隆。 So, I want to store all the data of users from the clone application to the realtime database in firebase. I have also created one realtime database(Test database) in Firebase and set the FirebaseDatabase dependency in my application.
所以,我想把克隆应用的所有用户数据存储到firebase的实时数据库中。我还在Firebase中创建了一个实时数据库(测试数据库),并在我的应用程序中设置了FirebaseDatabase依赖。 The problem is when I'm entering the data from my registration page and click on the register button, it was perfectly authenticating the data in Firebase but nothing is reflecting on the realtime database.
问题是当我从我的注册页面输入数据并单击注册按钮时,它完全验证了 Firebase 中的数据,但实时数据库没有任何反映。 Any suggestions will be perfect for me.
任何建议对我来说都是完美的。 Below, I'm sharing my codes following to this problem:
下面,我将针对此问题分享我的代码:
RegistrationActivity.java RegistrationActivity.java
package com.shankhadeep.firebaseinstagram;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
public class Registration extends AppCompatActivity {
private static final String TAG = "TAG";
EditText username, fullname, email, password;
Button btn_register;
TextView txt_login;
DatabaseReference mRootRef;
FirebaseAuth auth;
// FirebaseFirestore fStore;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
username = findViewById(R.id.username);
fullname = findViewById(R.id.fullname);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
btn_register = findViewById(R.id.btn_register);
txt_login = findViewById(R.id.txt_login);
pd = new ProgressDialog(this);
mRootRef = FirebaseDatabase.getInstance().getReference();
auth = FirebaseAuth.getInstance();
// fStore = FirebaseFirestore.getInstance();
txt_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Registration.this,Login.class));
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textUserName = username.getText().toString();
String textFullName = fullname.getText().toString();
String emailId = email.getText().toString();
String txt_Password = password.getText().toString();
if (TextUtils.isEmpty(textUserName) || TextUtils.isEmpty(textFullName) || TextUtils.isEmpty(emailId) || TextUtils.isEmpty(txt_Password)){
Toast.makeText(Registration.this,"Empty credentials", Toast.LENGTH_LONG).show();
}
else if(txt_Password.length() < 6) {
Toast.makeText(Registration.this, "Password too short!", Toast.LENGTH_LONG).show();
}
else {
registerUser(textUserName, textFullName, emailId, txt_Password);
startActivity(new Intent(Registration.this,Login.class));
finish();
}
}
});
}
private void registerUser(final String textUserName, final String textFullName, final String emailId, final String txt_password) {
pd.setMessage("Please Wait!");
pd.show();
auth.createUserWithEmailAndPassword(emailId , txt_password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
HashMap<String , Object> map = new HashMap<>();
map.put("name" , textUserName);
map.put("email", emailId);
map.put("username" , username);
map.put("id" , auth.getCurrentUser().getUid());
map.put("bio" , "");
map.put("imageurl" , "default");
mRootRef.child("Users").child(auth.getCurrentUser().getUid()).setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
pd.dismiss();
Toast.makeText(Registration.this, "Update the profile " +
"for better expereince", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Registration.this , MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pd.dismiss();
Toast.makeText(Registration.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
build.gradle(app level) build.gradle(应用级别)
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.shankhadeep.firebaseinstagram"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.annotation:annotation:1.0.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-functions:16.1.3'
implementation 'com.google.firebase:firebase-firestore:17.1.2'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation "com.hendraanggrian.appcompat:socialview:0.2"
implementation "com.hendraanggrian.appcompat:socialview-commons:0.2"
implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Here are some snapshots这是一些快照
Database creation and rules setup数据库创建和规则设置
Registration page in app应用程序中的注册页面
Data entered and registering数据输入和注册
first of all get user details if user is exits if not then you should update and if user is exits then update that one accroding to user id首先获取用户详细信息,如果用户退出,如果没有,那么您应该更新,如果用户退出,则根据用户 ID 更新该用户详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.