如何将用户配置文件信息存储在firebase数据库android中 [英] How to store user profile information in firebase database android

查看:165
本文介绍了如何将用户配置文件信息存储在firebase数据库android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个活动,将用户的信息存储在firebase数据库中。这是我已经修改的代码,但它没有在数据库中存储任何值没有退出按钮正在工作... ...
$ b $ p

activity_profile.xml

 <?xml version =1.0encoding =utf-8?< RelativeLayout xmlns:android =http://schemas.android .com / apk / res / android
xmlns:tools =http://schemas.android.com/tools
$ b $ android:layout_width =match_parent
android :layout_height =match_parent
android:paddingBottom =@ dimen / activity_vertical_margin
android:paddingLeft =@ dimen / activity_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context =www.edukeen.in.eduaspire.Home>

< ScrollView
android:layout_width =match_parent
android:layout_height =wrap_content>

< LinearLayout
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =vertical>

TextView
android:text =个人资料
android:layout_width =match_parent
android:layout_height =wrap_content
android :文字颜色= @颜色/ colorAccent/>

< TextView
android:text =Name
android:layout_width =match_parent
android:layout_height =wrap_content/>
$ b b编辑文本
android:id =@ + id / editTextName
android:layout_width =match_parent
android:layout_height =wrap_content/> ;

TextView
android:text =DOB
android:layout_width =match_parent
android:layout_height =wrap_content/>

编辑文本
android:id =@ + id / editDOB
android:layout_width =wrap_content
android:layout_height =wrap_content
android:inputType =date
android:ems =10/>

< TextView
android:text =Phone
android:layout_width =match_parent
android:layout_height =wrap_content/>

< EditText
android:id =@ + id / editphone
android:layout_width =wrap_content
android:layout_height =wrap_content
android:inputType =phone
android:ems =10/>

< TextView
android:text =City
android:layout_width =match_parent
android:layout_height =wrap_content/>

编辑文本
android:id =@ + id / editcity
android:layout_width =match_parent
android:layout_height =wrap_content/> ;

TextView
android:text =学术细节
android:layout_width =match_parent
android:layout_height =wrap_content
android :文字颜色= @颜色/ colorAccent/>

TextView
android:text =Class / year
android:layout_width =match_parent
android:layout_height =wrap_content/>

编辑文本
android:id =@ + id / editclass
android:layout_width =match_parent
android:layout_height =wrap_content/> ;

TextView
android:text =Board / Graduation degree
android:layout_width =match_parent
android:layout_height =wrap_content/>

< EditText
android:id =@ + id / editboard
android:layout_width =match_parent
android:layout_height =wrap_content/> ;

TextView
android:text =School / college
android:layout_width =match_parent
android:layout_height =wrap_content/>

< EditText
android:id =@ + id / editschool
android:layout_width =match_parent
android:layout_height =wrap_content/> ;

TextView
android:text =Hobbies
android:layout_width =match_parent
android:layout_height =wrap_content
android:的inputType = textMultiLine/>

编辑文本
android:id =@ + id / edithobbies
android:layout_width =match_parent
android:layout_height =wrap_content
android:inputType =textMultiLine/>

TextView
android:text =Achievements
android:layout_width =match_parent
android:layout_height =wrap_content/>
$ b $编辑文本
android:id =@ + id / editachievements
android:layout_width =match_parent
android:layout_height =wrap_content
android:inputType =textMultiLine/>

按钮
android:id =@ + id / buttonSave
android:text =保存
android:layout_width =match_parent
android:layout_height =wrap_content/>

< Button
android:id =@ + id / buttonsignout
android:text =退出
android:layout_width =match_parent
android:layout_height =wrap_content/>

< / LinearLayout>

< / ScrollView>





Profile.java

  public class Profile扩展AppCompatActivity {

私人按钮signOut;
私人ProgressBar progressBar;
私有FirebaseAuth.AuthStateListener authListener;
私人FirebaseAuth验证;

私人按钮buttonSave;

私人Firebase mref;

私人EditText editTextName;
private EditText editDOB;
private EditText editphone;
private EditText editcity;
private EditText editclass;
private EditText editboard;
私人EditText editschool;
私人EditText编辑;
private EditText editachievements;

FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener mAuthListener;

static final String TAG =YOUR-TAG-NAME;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Firebase.setAndroidContext(this);

editTextName =(EditText)findViewById(R.id.editTextName);
editDOB =(EditText)findViewById(R.id.editDOB);
editphone =(EditText)findViewById(R.id.editphone);
editcity =(EditText)findViewById(R.id.editcity);
editclass =(EditText)findViewById(R.id.editclass);
editboard =(EditText)findViewById(R.id.editboard);
editschool =(EditText)findViewById(R.id.editschool);
edithobbies =(EditText)findViewById(R.id.edithobbies);
editachievements =(EditText)findViewById(R.id.editachievements);

mref = new Firebase(https://*************.firebaseio.com/Users);
$ b mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
//用户登录
Log.d(TAG,onAuthStateChanged:signed_in:+ user.getUid());
} else {
//用户签出
Log.d(TAG,onAuthStateChanged:signed_out);
}
// ...
}
};

FirebaseUser userID = FirebaseAuth.getInstance()。getCurrentUser();
if(userID!= null){
buttonSave =(Button)findViewById(R.id.buttonSave);

buttonSave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){

String name = editTextName。 getText()。toString();
Firebase Childref1 = mref.child(Name);
Childref1.setValue(name);
$ b $ String DOB = editDOB.getText ).toString();
Firebase Childref2 = mref.child(DOB);
Childref2.setValue(DOB);

String Phone = editphone.getText()。 toString();
Firebase Childref3 = mref.child(Phone);
Childref3.setValue(Phone);

String City = editcity.getText()。toString );
Firebase Childref4 = mref.child(City);
Childref4.setValue(City);

String class = editclass.getText()。toString();
Firebase Childref5 =先生ef.child( 类);
Childref5.setValue(Class);

String Board = editboard.getText()。toString();
Firebase Childref6 = mref.child(Bord);
Childref6.setValue(Board);

String School = editschool.getText()。toString();
Firebase Childref7 = mref.child(学校);
Childref7.setValue(School);

String Hobbies = edithobbies.getText()。toString();
Firebase Childref8 = mref.child(Hobbies);
Childref8.setValue(Hobbies);

String Achievements = editachievements.getText()。toString();
Firebase Childref = mref.child(Achievements);
Childref.setValue(Achievements);
}
});
}

//注销开始
auth = FirebaseAuth.getInstance();
final FirebaseUser user = FirebaseAuth.getInstance()。getCurrentUser();
$ b authListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser userID = firebaseAuth.getCurrentUser();
if(userID == null){
startActivity(new Intent(Profile.this,LoginActivity.class));
finish();
}
}
};
}

public void signOut(){
auth.signOut();


@Override
protected void onResume(){
super.onResume();


@Override
public void onStart(){
super.onStart();
auth.addAuthStateListener(authListener);


@Override
public void onStop(){
super.onStop();
if(authListener!= null){
auth.removeAuthStateListener(authListener);


$ b code $ $ $ $ $ $ $ $ $ $ <$>屏幕





谁能告诉我我做错了什么?
提前感谢您

解决方案

一旦用户模型被配置文件细节填充b
$ b

 映射< String,Object> childUpdates = new HashMap<>(); 

myRef = database.getReference(parent_node ** strong text ** name)。child(user_profile_id);

myRef.child(user_profile_id).push();

childUpdates.put(user_profile_id,usermodel);

myRef.updateChildren(childUpdates);

以上代码生成具有用户标识的父节点,并将用户模型保存为该标识的值(即用户ID),稍后您可以通过在Firebase数据库中调用userid作为键来获取这些配置文件的详细信息。



希望这有助于:)

I am making an activity to store user'sinformation in firebase database. this is the code which i have trued, but it is not storing any value in databse neither signout button is working...

activity_profile.xml

<?xml version="1.0" encoding="utf-8"?<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="www.edukeen.in.eduaspire.Home">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:text="Personal details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"/>

        <TextView
            android:text="Name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editTextName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:text="DOB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editDOB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:ems="10"/>

        <TextView
            android:text="Phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editphone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:ems="10"/>

        <TextView
            android:text="City"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editcity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Academic details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"/>

        <TextView
            android:text="Class/year"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editclass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Board/Graduation degree"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editboard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:text="School/college"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editschool"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:text="Hobbies"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"/>

        <EditText
            android:id="@+id/edithobbies"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"/>

        <TextView
            android:text="Achievements"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editachievements"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"/>

        <Button
            android:id="@+id/buttonSave"
            android:text="Save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/buttonsignout"
            android:text="Sign out"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

Profile.java

public class Profile extends AppCompatActivity {

private Button signOut;
private ProgressBar progressBar;
private FirebaseAuth.AuthStateListener authListener;
private FirebaseAuth auth;

private Button buttonSave;

private Firebase mref;

private EditText editTextName;
private EditText editDOB;
private EditText editphone;
private EditText editcity;
private EditText editclass;
private EditText editboard;
private EditText editschool;
private EditText edithobbies;
private EditText editachievements;

FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener mAuthListener;

static final String TAG = "YOUR-TAG-NAME";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    Firebase.setAndroidContext(this);

    editTextName = (EditText) findViewById(R.id.editTextName);
    editDOB = (EditText) findViewById(R.id.editDOB);
    editphone = (EditText) findViewById(R.id.editphone);
    editcity = (EditText) findViewById(R.id.editcity);
    editclass = (EditText) findViewById(R.id.editclass);
    editboard = (EditText) findViewById(R.id.editboard);
    editschool = (EditText) findViewById(R.id.editschool);
    edithobbies = (EditText) findViewById(R.id.edithobbies);
    editachievements = (EditText) findViewById(R.id.editachievements);

    mref = new Firebase("https://*************.firebaseio.com/Users");

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

    FirebaseUser userID = FirebaseAuth.getInstance().getCurrentUser();
    if (userID != null) {
        buttonSave = (Button) findViewById(R.id.buttonSave);

        buttonSave.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View view){

                String name = editTextName.getText().toString();
                Firebase Childref1 = mref.child("Name");
                Childref1.setValue(name);

                String DOB = editDOB.getText().toString();
                Firebase Childref2 = mref.child("DOB");
                Childref2.setValue(DOB);

                String Phone = editphone.getText().toString();
                Firebase Childref3 = mref.child("Phone");
                Childref3.setValue(Phone);

                String City = editcity.getText().toString();
                Firebase Childref4 = mref.child("City");
                Childref4.setValue(City);

                String Class = editclass.getText().toString();
                Firebase Childref5 = mref.child("Class");
                Childref5.setValue(Class);

                String Board = editboard.getText().toString();
                Firebase Childref6 = mref.child("Bord");
                Childref6.setValue(Board);

                String School = editschool.getText().toString();
                Firebase Childref7 = mref.child("School");
                Childref7.setValue(School);

                String Hobbies = edithobbies.getText().toString();
                Firebase Childref8 = mref.child("Hobbies");
                Childref8.setValue(Hobbies);

                String Achievements = editachievements.getText().toString();
                Firebase Childref = mref.child("Achievements");
                Childref.setValue(Achievements);
            }
        });
    }

    //for signout start
    auth = FirebaseAuth.getInstance();
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    authListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser userID = firebaseAuth.getCurrentUser();
            if (userID == null) {
                startActivity(new Intent(Profile.this, LoginActivity.class));
                finish();
            }
        }
    };
}

public void signOut() {
    auth.signOut();
}

@Override
protected void onResume() {
    super.onResume();
}

@Override
public void onStart() {
    super.onStart();
    auth.addAuthStateListener(authListener);
}

@Override
public void onStop() {
    super.onStop();
    if (authListener != null) {
        auth.removeAuthStateListener(authListener);
    }
}
}

Screen

Can anyone tell me what I am doing wrong. Thank you in advance

解决方案

once user model gets filled by the profile details (feilds)

Map<String, Object> childUpdates = new HashMap<>();

myRef = database.getReference("parent_node**strong text**name").child(user_profile_id);

myRef.child(user_profile_id).push();

childUpdates.put(user_profile_id, usermodel);

myRef.updateChildren(childUpdates);

The above code generates parent node with user id and the user model gets saved as a value for that id(i.e user id) , you may later fetch those profile details by calling userid as a key in the Firebase Database

Hope it helps :)

这篇关于如何将用户配置文件信息存储在firebase数据库android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆