在android firebase

时间:2017-08-24 04:14:33

标签: android android-layout firebase firebase-realtime-database

我对编码本机很陌生。我一直在关注教程并将其改编为我自己的应用程序。

我的问题来自用户个人资料,在教程中,个人资料仅限于上传和更新用户个人资料照片以及更新用户名。

对于我的个人资料页面,我想介绍新数据。个人'员工'我想要存储在Firebase数据库中的数据。

我希望这些数据在输入时首先存储。然后,我希望它显示在配置文件页面的字段中,并且如果更改了值并单击了更新按钮,则可以更新它们。

The tutorial我一直关注的是udemy提供的课程

我也试过调整其他一些代码,没有运气。 (遗憾的是,我不能发布超过两个链接。请截取屏幕截图。)

These are the links to code I've tried adapting

下面列出的是配置文件布局的片段。首先是教程xml,然后是我想要的布局。

教程:

<ImageView
    android:id="@+id/userImageViewProfile"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:layout_gravity="center_horizontal"
    android:layout_margin="10dp"
    android:src="@mipmap/ic_launcher"
    />

<EditText
    android:id="@+id/userNameEditTextProfile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    />

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

矿:

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

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

        <ImageView
            android:id="@+id/userImageViewProfile"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher" />

        <EditText
            android:id="@+id/userNameProfile"
            android:layout_width="170dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="15dp"
            android:ems="10"
            android:hint="Name"
            android:inputType="textPersonName"
            android:textAlignment="center" />

        <EditText
            android:id="@+id/profTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:background="@drawable/rounded_fields"
            android:ems="10"
            android:hint="Profession"
            android:inputType="textPersonName"
            android:padding="10dp"
            android:textAlignment="center" />

        <EditText
            android:id="@+id/profReg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:background="@drawable/rounded_fields"
            android:ems="10"
            android:hint="Registration Number"
            android:inputType="number"
            android:padding="10dp"
            android:textAlignment="center" />

        <EditText
            android:id="@+id/profCall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:background="@drawable/rounded_fields"
            android:ems="10"
            android:hint="Contact Number"
            android:inputType="phone"
            android:padding="10dp"
            android:textAlignment="center" />

        <EditText
            android:id="@+id/profEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:background="@drawable/rounded_fields"
            android:ems="10"
            android:hint="Email Address"
            android:inputType="textEmailAddress"
            android:padding="10dp"
            android:textAlignment="center" />

        <EditText
            android:id="@+id/profID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:background="@drawable/rounded_fields"
            android:ems="10"
            android:hint="South African ID Number"
            android:inputType="number"
            android:padding="10dp"
            android:textAlignment="center" />

        <EditText
            android:id="@+id/profBank"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:background="@drawable/rounded_fields"
            android:ems="10"
            android:hint="Bank Account Number"
            android:inputType="number"
            android:padding="10dp"
            android:textAlignment="center" />

        <Button
            android:id="@+id/updateProfileButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:background="@drawable/rounded_button"
            android:text="Update"
            android:layout_marginBottom="25dp"/>

    </LinearLayout>

</ScrollView>

标题/职业,注册,联系电话,身份证号码和银行账户都是新领域。

感谢任何和所有帮助。

注意更新

我尝试过创建一个类并从那里开始工作。

类别:

public class UserInfo {

public String name;
public String title;
public String reg;
public String phone;
public String email;
public String id;
public String bank;

public UserInfo () {

}

public UserInfo(String name, String title, String reg, String phone, String email,String id,String bank) {
    this.name = name;
    this.title = title;
    this.reg = reg;
    this.phone = phone;
    this.email = email;
    this.id = id;
    this.bank = bank;
}

}

的活动:

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {

//firebase auth object
private FirebaseAuth mAuth;

//view objects
private EditText userNameProfile;
private EditText profEmail;

//defining a database reference
private DatabaseReference mUsersDB;

//our new views
private EditText profTitle, profReg, profCall, profID, profBank;
private Button updateProfileButton;

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

    mAuth = FirebaseAuth.getInstance();
    if(mAuth.getCurrentUser() == null){
        goToLogin();
    }

    //getting the database reference
    mUsersDB = FirebaseDatabase.getInstance().getReference();

    //getting the views from xml resource
    profTitle = (EditText) findViewById(R.id.profTitle);
    profReg = (EditText) findViewById(R.id.profReg);
    profCall = (EditText) findViewById(R.id.profCall);
    profID = (EditText) findViewById(R.id.profID);
    profBank = (EditText) findViewById(R.id.profBank);
    updateProfileButton = (Button) findViewById(R.id.updateProfileButton);

    updateProfileButton.setOnClickListener(this);

    FirebaseUser user = mAuth.getCurrentUser();

    //initializing views
    userNameProfile = (EditText) findViewById(R.id.userNameProfile);
    profEmail = (EditText) findViewById(R.id.profEmail);

    updateProfileButton.setOnClickListener(this);

}

@Override
protected void onResume() {
    super.onResume();
    if(mAuth.getCurrentUser() == null){
        goToLogin();
    }
}

private void goToLogin(){
    startActivity(new Intent(ProfileActivity.this, LoginActivity.class));

}

private void saveUserInformation() {
    //Getting values from database
    String title = profTitle.getText().toString().trim();
    String reg = profReg.getText().toString().trim();
    String phone = profCall.getText().toString().trim();
    String id = profID.getText().toString().trim();
    String bank = profBank.getText().toString().trim();


    //creating a userinformation object
    UserInfo userInfo = new UserInfo(title, reg, phone, id, bank);

    //getting the current logged in user
    FirebaseUser user = mAuth.getCurrentUser();

    //saving data to firebase database
    mUsersDB.child(user.getUid()).setValue(userInfo);

    //displaying a success toast
    Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG).show();
}

@Override
public void onClick(View view) {

    if(view == updateProfileButton){
        saveUserInformation();
    }

}

}

2 个答案:

答案 0 :(得分:0)

您可以在用户登录时将这些字段设为空白值。 喜欢

private static void swap(Stack<Integer> one, Stack<Integer> two) {
    Stack<Integer> temp = new Stack<>();
    temp.addAll(one);
    one.clear();
    one.addAll(two);
    two.clear();
    two.addAll(temp);
}

当用户更新这些值时,它将使用

更新firebase数据库中的特定值

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Users"); HashMap<String, String> userMap = new HashMap<String, String>(); userMap.put("name", ""); userMap.put("contact",""); userMap.put("ID Number",""); userMap.put("Profile Title",""); mDatabase.setValue(userMap); 代替 HashMap

答案 1 :(得分:0)

实际上,您可以创建一个UserInfo类,您可以在其中存储所需的文件。

    public class UserInfo { 

public String name;
public String title;
public String reg;
public String phone;
public String email;
public String id;
public String bank;

public UserInfo () { 

} 

public UserInfo(String name, String title,String reg, String email,String id,String bank) {
    this.name = name;
    this.title = title;
    this.reg = reg;
    this.phone = phone;
    this.email = email;
    this.id = id;
    this.bank = bank;
} 
}

当您想要保存数据时,只需创建此UserInfo类的对象并相应地指定值。

  UserInfo userInfo = new UserInfo(title, reg, phone, id, bank);
...

处理错误情况。

DatabaseReference mDatabase =FirebaseDatabase.getInstance().getReference().child("Users");
String key =user.getUid();
mDatabase = mDatabase.child(key);
mDatabase.setValue(userInfo );

它会将数据直接保存到具有唯一键的Users子项

我希望它会有所帮助