【发布时间】:2016-08-24 11:31:23
【问题描述】:
我使用 firebase 作为我的后端。用户正在使用电子邮件和密码进行身份验证。我正在使用推送将其他数据(例如姓名和电话)存储在实时数据库中。由于生成了唯一 ID,我如何检索用户数据以显示在用户个人资料上。 这是我存储数据的活动
public class UserActivity extends AppCompatActivity {
private EditText inputName, inputPhone, inputNatId,inputDOB;
private TextInputLayout inputLayoutName, inputLayoutPhone, inputLayoutNatId;
private Button Complete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inputName = (EditText) findViewById(R.id.input_names);
inputPhone = (EditText) findViewById(R.id.input_phone);
inputNatId = (EditText) findViewById(R.id.input_natid);
inputDOB = (EditText) findViewById(R.id.input_DOB);
inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_names);
inputLayoutPhone = (TextInputLayout) findViewById(R.id.input_layout_phone);
inputLayoutNatId = (TextInputLayout) findViewById(R.id.input_layout_natid);
Complete = (Button) findViewById(R.id.btncomplete);
Firebase.setAndroidContext(this);
Complete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase ref = new Firebase(Config.FIREBASE_URL);
String names = inputName.getText().toString();
String phone = inputPhone.getText().toString().trim();
String natid = inputNatId.getText().toString().trim();
String dob = inputDOB.getText().toString().trim();
if (TextUtils.isEmpty(names))
{
inputName.setError("Enter Full Names");
}
if (inputPhone.length() < 10 && inputPhone.length() > 10)
{
inputPhone.setError("Phone should be 10 digits");
}
if (inputNatId.length() < 8 && inputNatId.length() > 8)
{
inputNatId.setError("ID should be 8 digits");
}
if (TextUtils.isEmpty(phone))
{
inputPhone.setError("Enter Phone Number");
}
if (TextUtils.isEmpty(natid))
{
inputNatId.setError("Enter National ID");
}
if (TextUtils.isEmpty(dob))
{
inputDOB.setError("Enter Date Of Birth");
}
else
{
Firebase newref = ref.child("Person");
Map<String,String > users = new HashMap<String, String>();
users.put("names",names);
users.put("phone",phone);
users.put("national id",natid);
users.put("dob",dob);
newref.push().setValue(users);
Firebase postref = newref.push();
String postID = postref.getKey();
if (true){
Runnable runnable = new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Registration Successful",Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
startActivity(intent);
finish();
}
};
android.os.Handler handler = new android.os.Handler();
handler.postDelayed(runnable, 1000);
}
else {
Toast.makeText(getApplicationContext(),"Registration Error",Toast.LENGTH_LONG).show();
}
}
}
});
}
这是正在检索数据的活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
auth = FirebaseAuth.getInstance();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference();
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View navheaderview = navigationView.inflateHeaderView(R.layout.nav_header_main2);
ProfilePic = (ImageView) navheaderview.findViewById(R.id.imguserpic);
Name = (TextView) navheaderview.findViewById(R.id.tvusername);
Email = (TextView) navheaderview.findViewById(R.id.tvuseremail);
Email.setText(user.getEmail());
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorlayout);
Snackbar snackbar = Snackbar.make(coordinatorLayout, "Welcome " + user.getEmail(), Snackbar.LENGTH_LONG);
snackbar.setAction("SignOut", new View.OnClickListener() {
@Override
public void onClick(View v) {
SignOut();
}
});
snackbar.show();
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null){
startActivity(new Intent(Main2Activity.this, LoginActivity.class));
finish();
}
}
};
PermissionRequests();
}
这是我的实时数据库
barapp-689c1
Person
-KP2YEDe3nhzlW0s4ckH
dob: 27/04/1996
names: Mark K
national id: 01234567
phone: 0727286122
-KP3bKyKGh_PeHCEisjG
dob: 30/6/1990
names: Bruno M
national id: 12345698
phone: 0721241201
我应该使用这种方法还是应该使用set方法
【问题讨论】:
标签: android firebase-realtime-database firebase-authentication