如果有人可以帮助我,将不胜感激。我是Firebase的初学者,这是我在学校的最后一个项目。我一直在处理这个问题好几个星期,而我认为已经解决了近一个月。请忍受我。
我的最终项目中有“ Auth.GoogleSignInApi”方法。一切顺利。我试图将每个用户及其“ getUid”存储在firebase数据库中,并为他们创建了一个小结构,例如电话号码,昵称和地址。当用户通过Google帐户登录时,我创建了三个子项,分别称为“个人资料,邮件和其他项”。
Query query = database_Reference
.orderByChild("Profile/pro_ID")
.equalTo(user.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
Toast.makeText( Activity_Login.this, "Create new username ", Toast.LENGTH_SHORT ).show();
Record_ID_FromServer = database_Reference.child(user.getUid()).getKey();//.push().getKey();
database_Reference.child( Record_ID_FromServer ).child("Profile").setValue( profile_user_ );
database_Reference.child( Record_ID_FromServer ).child("Messages").setValue( profile_user_);
database_Reference.child( Record_ID_FromServer ).child("Other").setValue( profile_user_);
“个人资料项目”有七个项目,其中一个项目有三个项目,我给它起了一个名字“ t_address”。
有时,用户有第二个电话号码或地址。但是每当我尝试部署“ DataSnapshot postSnapshot”时!我无法检索“ t_address”的“ ListView”。我一直在寻找并遵循许多教程,但都没有用。
Firebase中的结构如下: 1:“ Record_ID_FromServer = database_Reference.child(user.getUid())。getKey();”
2: “ UserInfoAddres s = new UserInfoAddres();” “ myRef.child(user.getUid())。child(” Profile“)。child(” t_address“)。setValue(s);”
我的问题是如何获取“ t_address”并将其显示在ListView中?
ListView的Java类:
public class Address_listview extends AppCompatActivity {
private static final String TAG = "Address_listview";
TextView t_add_listview_;
String S_t_add_listview_;
ListView listview;
// ===============================================
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
String userID;
FirebaseUser user;
// ================================================
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.address_listview );
Objects.requireNonNull( getSupportActionBar() ).setDisplayHomeAsUpEnabled( true );
// ================================================
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference().child( "users" );
user = mAuth.getCurrentUser();
userID = user.getUid();
// ================================================
t_add_listview_ = (TextView) findViewById( R.id.text_address_listview );
S_t_add_listview_ = t_add_listview_.getText().toString();
listview = (ListView) findViewById( R.id.address_listView_list );
// ================================================
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)
{
FirebaseUser user = firebaseAuth.getCurrentUser();
//If user is not signed in getCurrentUser method returns null
if (user != null) {
Log.d( TAG, "onAuthstateChange:signed:" + user.getUid() );
Log.d( "TAG", "You are signed in Firebase!" );
// User is signed in
// Toast.makeText( MainActivity.this, "FirebaseAuth user if", Toast.LENGTH_SHORT ).show();
//UserTV.setText( user.getEmail().toString() );
t_add_listview_.setText( user.getEmail().toString() );
}
}
};
// ================================================
}
// ================================================
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu, menu);
// Associate searchable configuration with the SearchView
// SearchManager searchManager = (SearchManager) getSystemService(Activity_Login.this.SEARCH_SERVICE);
// SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
if (menu != null) {
menu.findItem(R.id.menu_search).setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(Address_listview.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return true;
default:
return super.onOptionsItemSelected( item );
}
}
// ================================================
// ================================================
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
// ================================================
}
和其他此类添加内容,例如城市,区域和邮政编码
// Query query_Address = myRef
// .orderByChild("Profile/pro_ID")
// .equalTo(user.getUid());
myRef.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String get_Address_string1 = myRef.push().getKey();
String postkey = postSnapshot.getRef().getKey();
String get_Address_string = myRef.child(user.getUid()).getKey();
// Database_Reference_Edit_Address.child( postkey ).child( "Profile" ).child( "Address" ).child( get_Address_string ).child( "_UserAddress" ).setValue( S_editText_address );
//Database_Reference_Edit_Address.child(postkey).child("Profile").child("_t_address").child( get_Address_string ).setValue(user_address);
//get data from edittexts
String city = cityEditTxt.getText().toString();
String area = areaEditText.getText().toString();
String zipcode = zipcodeEditText.getText().toString();
//set data to POJO
UserInfoAddres s = new UserInfoAddres();
s.setcity(city);
s.setarea(area);
s.setzipcode(zipcode);
myRef
.child(user.getUid())
//.child( postkey )
.child( "Profile" )
.child( "t_address" )
.child( get_Address_string1 )
.setValue( s );
Toast.makeText( M_MainActivity.this, "added ", Toast.LENGTH_SHORT ).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
// =========================================================================================
}
});
和此类
public class UserInfoAddres {
private String city, area, zipcode;
public UserInfoAddres() {
}
public UserInfoAddres(String city, String area, String zipcode) {
this.city = city;
this.area = area;
this.zipcode = zipcode;
}
public String getcity() {
return city;
}
public void setcity(String city) {
this.city = city;
}
public String getarea() {
return area;
}
public void setarea(String area) {
this.area = area;
}
public String getzipcode() {
return zipcode;
}
public void setzipcode(String zipcode) {
this.zipcode = zipcode;
}
}
亲切的问候,
答案 0 :(得分:0)
当我决定问或问的时候,我不知道那个秘密是什么,立即我从某个地方得到了解决方案。
我只添加这段代码
databaseReference.child("users").child(userID).child("Profile").child("t_address").push().setValue(user);
还有这个
databaseReference.child("users").child(userID).child("Profile").child("t_address").addChildEventListener(new ChildEventListener() {