这是我的数据库结构
我想允许用户在他们的个人资料中更新他们的信息,以便我如何实现这一目标。我知道我首先需要从firebase读取信息然后处理更新它,但目前我在从firebase读取数据时遇到问题。
我已经附加了我的数据库结构和代码。
package faz.ie.partyapp;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import faz.ie.partyapp.models.User;
import faz.ie.partyapp.models.updateUserInformation;
public class UserProfile extends AppCompatActivity {
ImageView image;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mReference;
String userid;
private TextView mAgeTextView;
private TextView mGenderTextView;
private TextView mPhoneNumberTextView;
private TextView mFullNameTextView;
private TextView mEmailTextView;
private Button mEditButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
image = (ImageView)findViewById(R.id.userImage);
mAgeTextView = (TextView)findViewById(R.id.updateAgeTxt);
mGenderTextView = (TextView)findViewById(R.id.updateGenderTxt);
mPhoneNumberTextView = (TextView)findViewById(R.id.updatePhoneTxt);
mFullNameTextView = (TextView)findViewById(R.id.updateNameTxt);
mEmailTextView = (TextView)findViewById(R.id.updateEmailTxt);
mEditButton = (Button) findViewById(R.id.editButton);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
mReference = mFirebaseDatabase.getReference();
FirebaseUser user = mAuth.getCurrentUser();
userid = user.getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersReference = rootRef.child("userType");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
User userInformation = new User();
userInformation.setEmailAddress(ds.child(userid).getValue(User.class).getEmailAddress());
userInformation.setFullName(ds.child(userid).getValue(User.class).getFullName());
userInformation.setAge(ds.child(userid).getValue(User.class).getAge());
userInformation.setPhoneNumber(ds.child(userid).getValue(User.class).getPhoneNumber());
userInformation.setGender(ds.child(userid).getValue(User.class).getGender());
Log.d("TAG", userInformation.getFullName() + " / " + userInformation.getEmailAddress() + " / " + userInformation.getAge()+
" / " + userInformation.getPhoneNumber() + " / " + userInformation.getGender());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersReference.addListenerForSingleValueEvent(eventListener);
mEditButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// intent to go to the edit/update user information activity
}
});
}
}
答案 0 :(得分:0)
in:
DatabaseReference usersReference = rootRef.child("userType");
“userType”似乎不是可用的东西......
答案 1 :(得分:0)
为了解决这个问题,您需要稍微更改数据库结构。删除额外节点Host a Party
和Look for a Party
,并将这些类型添加为用户的属性。您的数据库结构应如下所示:
Firebase-root
|
--- users
|
--- uid
|
--- "userType": "Host a Party"
|
--- //the other user details
要显示所有用户,只需使用以下内容:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String fullName = ds.child("FullName").getValue(String.class);
Log.d("TAG", fullName);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);
如果您只想显示looking for a party
而非usersRef
的用户,则需要使用以下Query
:
Query query = usersRef.orderByChild("userType").equaltTo("Host a Party");
query.addListenerForSingleValueEvent(valueEventListener);
如果您只想显示经过身份验证的用户的详细信息,请使用以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String fullName = dataSnapshot.child("FullName").getValue(String.class);
Log.d("TAG", fullName);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
删除旧数据和新数据。