Android: Why the value from firebase database is not being stored properly?

Android: Why the value from firebase database is not being stored properly?



The code I am using to store the value of uid child from firebase database is as following:


uid


lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)

String data = (String) parent.getItemAtPosition(position);
usersdRef.orderByChild("username").equalTo(parent.toString()).addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
for(DataSnapshot data: dataSnapshot.getChildren())
String dataUID = data.child("uid").getValue(String.class);
toUid = dataUID;



@Override
public void onCancelled(@NonNull DatabaseError databaseError)


);

Intent intent = new Intent(Main2Activity.this, Main5Activity.class);
intent.putExtra("valueName", data);
intent.putExtra("valueUID",toUid);
startActivity(intent);


);



But when I try to use the value in different activity, the value is passed as null. I obtain and use the value as:


Bundle bundle = getIntent().getExtras();
assert bundle != null;
final String hName = bundle.getString("valueName");
final String td = bundle.getString("valueUID");



hName is stored perfectly but td is passed as null.


hName


td



The firebase database looks something like this:



firebase database





What is the value of parent.toString() from your reference?
– Alex Mamo
Aug 30 at 10:50



parent.toString()





@AlexMamo it is the clicked username from the list of all usernames in the activity.
– user10158599
Aug 30 at 11:09




2 Answers
2



It is because you are trying to access value of variable toUid outside the actual callback. Callback meant to be async, meaning you can not just access it's result on the next line after it's definition. Your code is ok, but as an idea, access this variable inside callback method, not beneath the callback itself


toUid


lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)

String data = (String) parent.getItemAtPosition(position);
usersdRef.orderByChild("username").equalTo(parent.toString()).addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
for(DataSnapshot data: dataSnapshot.getChildren())
String dataUID = data.child("uid").getValue(String.class);
toUid = dataUID;

Intent intent = new Intent(Main2Activity.this, Main5Activity.class);
intent.putExtra("valueName", data);
intent.putExtra("valueUID",toUid);
startActivity(intent);



@Override
public void onCancelled(@NonNull DatabaseError databaseError)


);

);



To solve this problem, please use the following code:


lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)

String data = (String) parent.getItemAtPosition(position);

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 dataUID = dataSnapshot.child("uid").getValue(String.class);
String dataUsername = dataSnapshot.child("username").getValue(String.class);

Intent intent = new Intent(Main2Activity.this, Main5Activity.class);
intent.putExtra("valueUID",dataUID);
intent.putExtra("valueName", dataUsername);
startActivity(intent);


@Override
public void onCancelled(@NonNull DatabaseError databaseError)
Log.d(TAG, databaseError.getMessage());

;
uidRef.addListenerForSingleValueEvent(valueEventListener);

);



Instead of query the entire database to find a single user, which is a waste of time and resources you can get a reference of that particular user using his uid. So there is no need to use getChildren() method to loop through the DataSnapshot object, since you can simply get it without a loop. It is true, onDataChange() method has an asynchronous behavior and all the code regarding the intent should placed inside the callback. If you want to use those values outside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.


uid


getChildren()


DataSnapshot


onDataChange()





Hey, thanks for the answer, but I do need to loop and see through the usernames because I have to display that in next activity, I placed those within the same code block, and that works. Thank you.
– user10158599
Aug 31 at 8:16





In order to display that username in another activity, there is no need to loop. You can only get a single DataSnapshot, that's it.
– Alex Mamo
Aug 31 at 8:20





But good to hear that it worked! If you think that my answer helped you, please consider accepting it by clicking the checkmark on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
– Alex Mamo
Aug 31 at 8:20



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế