gpt4 book ai didi

java - 如何获取用户当前登录的帖子的 key

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:17 25 4
gpt4 key购买 nike

我想获取用户当前登录的帖子 ID,即在我的情况下,我需要这个:“-Kqm0-HrwGNgDhvTqOjU”,因为当前用户 ID 是“OiHtZxtLUeh63UzudUChEpSODPx2”。

my database structure

以便我可以获得当前登录用户的名称

这是我的代码:

public class MyAccount extends AppCompatActivity {

private Button mSetupButton;
private EditText mSetupName;
private EditText mSetupBio;
private ImageButton mSetupImageButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private String mPostKey=null;
private DatabaseReference mDatabase;
private FirebaseDatabase mFirebasedatabase;
private DatabaseReference myRef;
private String userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_account);

DatabaseReference mdatabaseR = FirebaseDatabase.getInstance().getReference().child("Profiles");


mAuth = FirebaseAuth.getInstance();
mFirebasedatabase=FirebaseDatabase.getInstance();

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
//Toast.makeText(this, "" + currentFirebaseUser.getUid(), Toast.LENGTH_SHORT).show();




mSetupName = (EditText) findViewById(R.id.accountName);
mSetupBio = (EditText) findViewById(R.id.accountBio);
mSetupButton = (Button) findViewById(R.id.accountButton);
mSetupImageButton = (ImageButton) findViewById(R.id.accountImageButton);

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Profiles");


String user_id=mAuth.getCurrentUser().getUid();
DatabaseReference myRef=ref.child(user_id);


ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot objSnapshot: snapshot.getChildren()) {
String obj = objSnapshot.getKey();
Toast.makeText(MyAccount.this,obj, Toast.LENGTH_LONG).show();

String post_name = (String) objSnapshot.child("Name").getValue();
mSetupName.setText(post_name);

}
}
@Override
public void onCancelled(DatabaseError firebaseError) {

}
});



//Toast.makeText(MyAccount.this,user_id,Toast.LENGTH_LONG).show();

}

}

在“String obj”中,由于 for 循环,我获得了所有帖子 id,但在我的情况下,我只需要用户登录的帖子,即其 uid 为“OiHtZxtLUeh63UzudUChEpSODPx2”

最佳答案

你必须循环每个对象,如下所示:

ref.child('users').orderByChild('uid').equalTo('your_uid').on("value", 
function(snapshot) {
console.log(snapshot.val());

});

关于java - 如何获取用户当前登录的帖子的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com