Stack Overflow Asked by ictwinner on December 7, 2021
This is my java code:
public void updateWalletString(){
User user = (User)Homeactivity.getUser()
wallet = user.getWallet()
String walletKey = getAWalletKey(wallet);
Log.v(TAG, walletKey);
}
private String getAWalletKey(Wallet w) {
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
databaseReference.child("Users").child(uid).child("wallets").orderByChild("name")
.equalTo(walletname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
WalletKey[0] = childSnapshot.getKey();
Log.d(TAG, "getWalletkey: "+ WalletKey[0]);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomeActivity.this, "Error retrieving walletId from database", Toast.LENGTH_SHORT);
}
});
return WalletKey[0];
}
But my logcat printed:
D/HomeActivity: getAWalletKey: Cash Wallet (this line comes from the getAWalletKey() function )
V/HomeActivity: null (this line comes from the updateWalletString() function)
I checked the code, everything is correct.
There are currently two different thread executing in your Code. The first was your main Thread that executes from
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
return WalletKey[0];
and the asynchronous thread that fetching the Data from the firebase.
By the time you returning the WalletKey[0] the value is still null
, because the asynchronous process not finished yet.
I think the best way to overcome this is to change the getAWalletKey
function to alter the wallet. Like this.
public void updateWalletString(){
User user = (User)Homeactivity.getUser()
wallet = user.getWallet()
updateAWalletKey(wallet);
}
private void getAWalletKey(Wallet w) {
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
databaseReference.child("Users").child(uid).child("wallets").orderByChild("name")
.equalTo(walletname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
WalletKey[0] = childSnapshot.getKey();
Log.v(TAG, walletKey);
Log.d(TAG, "getWalletkey: "+ WalletKey[0]);
doTheWallet(childSnapshot.getKey()); // whatever you want to do with the wallet.
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomeActivity.this, "Error retrieving walletId from database", Toast.LENGTH_SHORT);
}
});
return WalletKey[0];
}
Answered by Miftahun Najat on December 7, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP