【发布时间】: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