【问题标题】:Store, display and update new data in android firebase在 android firebase 中存储、显示和更新新数据
【发布时间】:2017-08-24 04:14:33
【问题描述】:

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

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

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

我希望在输入时首先存储这些数据。然后,我希望它显示在个人资料页面上的字段中并更新它们,如果值发生更改并单击更新按钮。

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

我也尝试过修改一些其他代码,但没有成功。 (很遗憾,我不能发布两个以上的链接。为截图道歉。)

These are the links to code I've tried adapting

下面列出的是配置文件布局的 sn-ps。首先是教程 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();
    }

}

}

【问题讨论】:

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


    【解决方案1】:

    您可以在用户登录时将这些字段留空。 喜欢

       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);
    

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

    更新 firebase 数据库中的特定值

    Map updateMap = new HashMap(); 而不是 HashMap

    【讨论】:

      【解决方案2】:

      实际上,您可以创建一个 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子中

      希望对你有帮助

      【讨论】:

      • 嗨,我在大约 10 分钟前尝试过类似的操作。创建用户信息对象时遇到错误。 “UserInfo 是抽象的;无法实例化。”我不确定我是否做得对?
      • 创建您自己的名为 Employee 的类,就像我在回答中创建的那样
      • 我有。我的班级只是称为“UserInfo”而不是“Employee”
      • 然后您需要扩展 UserInfo 并创建一个新类,您将在其中添加字段。
      • 我更新了我的帖子,如果它能让我更容易理解我所做的事情。
      猜你喜欢
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2021-12-13
      相关资源
      最近更新 更多