Firebase data retrieving issue in android studio - java

I want to retrieve data from firebase and display it on recycle view. I provided the correct path for data retrieving. But there is some problem i am unable to find it.
This code where i provided the child address.
final DatabaseReference nm= FirebaseDatabase.getInstance().getReference("Cart")
.child("Admin view")
.child(phoneNo)
.child("Products");
nm.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot npsnapshot : dataSnapshot.getChildren())
{
cart l=npsnapshot.getValue(cart.class);
listData.add(l);
}
adapter = new cartAdapterr(listData, AdminShowOrderProductsActivity.this);
rv.setAdapter(adapter);
}
else
{
Toast.makeText(AdminShowOrderProductsActivity.this, "No Data for: " + phoneNo, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
That is the screenshot of my firebase database and emulator. the phone number in toast message which is also present in firebase database. on node Phone number is correct but it shows error.

The way you work is correct, but you have a mistake, which is when the data is modified in Firebase, a new cartAdapterr is created and this operation is wrong.
You must first create an Adapter and then send the data.
for example you can create it onCreate and create a method inside the Adapter that receives List <Cart> as Shown below :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//..
adapter = new cartAdapterr(this);
loadDataFirebase():
}
void loadDataFirebase(){
final DatabaseReference nm= FirebaseDatabase.getInstance().getReference("Cart")
.child("Admin view")
.child(phoneNo)
.child("Products");
nm.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot npsnapshot : dataSnapshot.getChildren())
{
cart l=npsnapshot.getValue(cart.class);
listData.add(l);
}
adapter.setDataList(listData);
}
else
{
Toast.makeText(AdminShowOrderProductsActivity.this, "No Data for: " + phoneNo, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
In Adapter you have to create this setDataList (List<Cart> cartItems) :
public void setDataList (List<Cart> cartItems ) {
this.cartItems = cartItems;
notifyDataSetChanged();
}

Related

How to ensure Completion handler for one Firebase lookup completes before another one does

I have the following Firebase DB node structure:
UserInGroup
--- GroupID
--- UserId : true/false
Users
--- UserId
--- Username : String
--- ...
GroupStatus
--- GroupId
--- UserId: true/false
I need to pull for the first node to get all the users in the Group
Then use that info to get the users account info details
Finally check to see the users status in the Group
I cannot figure a way to implement the completionhandler in Java/Android ? I have done so for iOS with completionhandlers.
Can anyone assist with helping me implement the solution in Java?
---- UPDATE ----
I have done the following:
// Create an interface to init all the callback functions
private interface AllUsersCallback {
void onSuccess(DataSnapshot dataSnapshot);
void onStart();
void onFailure();
}
private void readData(Query query, AllUsersActivity.AllUsersCallback listener) {
listener.onStart();
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
listener.onSuccess(dataSnapshot);
} else { // dataSnapshot doesn't exist
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
//
listener.onFailure();
}
});
}
And lastly the Activity view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Init ArrayList
userList = new ArrayList<>();
userInGroupReference = mFirebaseDatabase.getReference("GroupUsers");
userInGroupQuery = userInGroupReference.child(groupID).orderByValue().equalTo(true);
// Completion Handler for Lookups
readData(userInGroupQuery, new AllUsersActivity.AllUsersCallback() {
#Override
public void onSuccess(DataSnapshot dataSnapshot) {
// Clear the List (remove dupes)
userList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String userId = snapshot.getKey();
// Call function to set usernames to the users
setUsername(userId);
}
/*
THIS ALWAYS COMES OUT BLANK!? <--------
*/
for (int i = 0; i < userList.size(); i++) {
Log.e(TAG,"List element: " + userList.get(i).getUsername());
}
}
#Override
public void onStart() {
// When starting
Log.d("ONSTART", "Started");
}
#Override
public void onFailure() {
// If failed
Log.d("onFailure", "Failed");
}
});
}
and the function used to set the users username to the userList:
public void setUsername(String userId) {
userReference = mFirebaseDatabase.getReference("Users");
userQuery = userReference.child(userId).child("username");
// Add handle for listener
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String username = dataSnapshot.getValue().toString();
AllUsers result = new AllUsers(username);
userList.add(result);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
These database calls are asynchronous - the callback code does not run immediately, it runs some time in the future when you actually get the data.
The easiest way to chain multiple dependent async queries is to put each query into its own function, and call it from the dependent query's callback. In your case, you could have multiple callbacks running at once, so as each one completes you can check for it to be done and check for them all to be done by comparing the size of the list with the number of queries launched.
For example:
private ArrayList<String> userList = new ArrayList<>();
private int numUsers = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// other setup stuff
startInitialQuery();
}
private void startInitialQuery() {
// make your initial query
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
userList.clear();
numUsers = 0; // dataSnapshot.getChildren().size();
// If the size() call above works, use that, otherwise
// you can count the number of children this way.
for(DataSnapshot snap : dataSnapshot.getChildren()) {
++numUsers;
}
for(DataSnapshot snap : dataSnapshot.getChildren()) {
String userId = snap.getKey();
readUser(userId);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
});
}
private void readUser(String userId) {
// make userQuery using "userId" input
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String username = dataSnapshot.getValue().toString();
userList.add(username);
checkLoaded();
}
else {
--numUsers;
checkLoaded();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
--numUsers;
checkLoaded();
}
});
}
private void checkLoaded() {
if( userList.size() == numUsers ) {
// All done getting users! Show a toast, update a view, etc...
}
}
Alternately, if you switch to using Kotlin and coroutines you can write this as a pretty simple linear suspend function where you can actually make the different tasks wait.
A cleaner, but more invasive change, would be to move this all to a ViewModel that contains LiveData of each of these steps. As data is received, you post it to the LiveData and the UI can observe that and react accordingly (e.g update views, trigger the next call, etc).
Update
Here is an example showing how to do this with a ViewModel and LiveData
public class MainViewModel extends ViewModel {
private final MutableLiveData<List<String>> users = new MutableLiveData<>();
LiveData<List<String>> getUsers() {
return users;
}
private final ArrayList<String> userList = new ArrayList<>();
void startFetchingData() {
// build query
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
userList.clear();
for(DataSnapshot snap : dataSnapshot.getChildren()) {
String userId = snap.getKey();
readUser(userId);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
});
}
private void readUser(String userId) {
// build userQuery
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String username = dataSnapshot.getValue().toString();
userList.add(username);
users.postValue(userList);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
});
}
}
and in the activity you set an observer for the LiveData that is notified any time the data changes.
model = new ViewModelProvider(this).get(MainViewModel.class);
final Observer<List<String>> userObserver = userList -> {
// Update the UI, or call something else
// this will get called every time the list of users is
// updated in the ViewModel
System.out.println("TEST: got data " + userList);
};
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
model.getUsers().observe(this, userObserver);
model.startFetchingData();

Add extra data to recyclerView

I've just recently started to learn some coding, a little python and java.
I'm trying to make a whatsapp clone as a test, and I've hit a wall.
Basically I have a UID showing as the name of a group chat, and I want to swap it for a groupId I have saved in firebase at the same level as the Uid.
FirebaseDatabase.getInstance().getReference().child("chat").child(chatId).child("info").child("groupId)
I've been following simcoder youtube video and there's a lot of checks and balances happening between user and chat UIDs and I'm getting lost in the middle of all that.
I've tried adding groupId to the adapter and the chatObject, and I can see the data in debug, but I just can't get it to populate the recycler view. I've had a search of the forums here, but can't quite get it to work.
I'm hoping someone can help me out, many thanks
private RecyclerView mChatList;
private RecyclerView.Adapter mChatListAdapter;
private RecyclerView.LayoutManager mChatListLayoutManager;
ArrayList<ChatObject> chatList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_chat);
OneSignal.startInit(this).init();
OneSignal.setSubscription(true);
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
#Override
public void idsAvailable(String userId, String registrationId) {
FirebaseDatabase.getInstance().getReference().child("user").child(FirebaseAuth.getInstance().getUid()).child("notificationKey").setValue(userId);
}
});
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
Button mLogout = findViewById(R.id.logout);
Button mFindUser = findViewById(R.id.finduser);
mFindUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), FindUserActivity.class));
}
});
mLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
OneSignal.setSubscription(false);
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return;
}
});
getPermissions();
initializeRecyclerView();
getUserChatList();
}
private void getUserChatList(){
DatabaseReference mUserChatDB = FirebaseDatabase.getInstance().getReference().child("user").child(FirebaseAuth.getInstance().getUid()).child("chat");
mUserChatDB.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()){
ChatObject mChat = new ChatObject(childSnapshot.getKey());
boolean exists = false;
for (ChatObject mChatIterator : chatList){
if(mChatIterator.getChatId().equals(mChat.getChatId()))
exists = true;
}
if (exists)
continue;
chatList.add(mChat);
getChatData(mChat.getChatId());
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void getChatData(String chatId) {
DatabaseReference mChatDB = FirebaseDatabase.getInstance().getReference().child("chat").child(chatId).child("info");
mChatDB.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String chatId = "";
if(dataSnapshot.child("id").getValue() != null)
chatId = dataSnapshot.child("id").getValue().toString();
for(DataSnapshot userSnapshot : dataSnapshot.child("users").getChildren()){
for(ChatObject mChat : chatList)
if(mChat.getChatId().equals(chatId)){
UserObject mUser = new UserObject(userSnapshot.getKey());
mChat.addUserToArrayList(mUser);
getUserData(mUser);
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void getUserData(UserObject mUser) {
DatabaseReference mUserDb = FirebaseDatabase.getInstance().getReference().child("user").child(mUser.getUid());
mUserDb.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserObject mUser = new UserObject(dataSnapshot.getKey());
if(dataSnapshot.child("notificationKey").getValue() != null)
mUser.setNotificationKey(dataSnapshot.child("notificationKey").getValue().toString());
for(ChatObject mChat : chatList)
for(UserObject mUserIt : mChat.getUserObjectArrayList()){
if(mUserIt.getUid().equals(mUser.getUid())){
mUserIt.setNotificationKey(mUser.getNotificationKey());
}
}
mChatListAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void initializeRecyclerView() {
chatList = new ArrayList<>();
mChatList = findViewById(R.id.chatList);
mChatList.setNestedScrollingEnabled(false);
mChatList.setHasFixedSize(false);
mChatListLayoutManager = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL,false);
mChatList.setLayoutManager(mChatListLayoutManager);
mChatListAdapter = new ChatListAdapter(chatList);
mChatList.setAdapter(mChatListAdapter);
}
From what I see you are passing to the adapter an empty list so that's why there's not data to be displayed.
And also, where you are using mChatListAdapter.notifyDataSetChanged();, you don't set before a populated list to the adapter.
I'm not sure which is the data you want to display but you have to collect in a list from the firebase all the information you want to display, for example, if UserObject is what you want, you have to make an ArrayList<UserObject>, to add there all the objects, pass it to the adapter and then call adapter.notifyDataSetChanged() .
You can make function in the adapter, like :
public void setData(ArrayList<YourObject> list){
yourAdapterList = list
this.notifyDataSetChanged()
}
And then call it from your activity/fragment like adapter.setData( /* your populated list */)

Firebase query logic not working in android

I want to make a query that checks whether a certain name (President or Secretary) exists inside a database.
The structure of the database is as follows.
I have this code, but its not working. Is there something I'm doing wrong?
mDatabase = FirebaseDatabase.getInstance().getReference();
Query presidentquery = mDatabase.child("validate").child(uid).equalTo("President");
presidentquery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Candidate p = dataSnapshot1.getValue(Candidate.class);
president.setEnabled(false);
president.setText("Voted Already");
}
}
else{
president.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Home.this, AllCandidates.class));
finish();
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
It seems you know the exact node you want to load, in which case you don't need an equalTo. Instead you can look up the node with:
Query presidentquery = mDatabase.child("validate").child(uid).child("President");
The rest of your code can stay the same.

Getting values from Firebase

I'm new to Firebase, and decided to get my feet wet. However, I'm having trouble retrieving values from a query. I'm basically trying to get the password value, but I believe it's returning nothing.
Error:
java.lang.NullPointerException: println needs a message
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
db.child("Users").orderByChild("username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//Get the password,and check it against the password field
Map<String, String> map = (Map<String, String>)dataSnapshot.getValue();
Log.d("result", map.get("password"));
} else {
Toast.makeText(getApplicationContext(), "Incorrect login details", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
I know this isn't very practical, but like I said I'm only practicing.
Thanks for the help!
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
The code in your onDataChange needs to take care of the fact that the snapshot is a list, by looping over its snapshot.getChildren():
db.child("Users").orderByChild("username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
//Get the password,and check it against the password field
Map<String, String> map = (Map<String, String>)snapshot.getValue();
Log.d("result", map.get("password"));
}
}
Sine your database structure is different than your reference in your code you should change some things
First your Reference to the data should be like this
db.child("Users").child(uid).child("password").addValueEventListener...
Where uid is the result of
private FirebaseAuth mAtuh;
mAuth = FirebaseAuth.getCurrentUser().getUid();
String uid = mAuth ;
Check the official doc on how to properly authenticate users
https://firebase.google.com/docs/auth/android/manage-users
Also you can implement google sign-in :
https://firebase.google.com/docs/auth/android/google-signin
And then you can retrieve your password field like this
// Attach a listener to read the data
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String password = dataSnapshot.getValue(String.class);
Log.e("The password is:",""+password);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("Error getting the password",""+databaseError.getCode());
}
});
Hardcoding your reference without mAuth will be like this
db.child("Users").child("L7VR2mGZKnbReFqOlmP").child("password").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String password = dataSnapshot.getValue(String.class);
Log.e("The password is:",""+password);
}else
{
Toast.makeText(getApplicationContext(), "Incorrect login details", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("Error getting the password",""+databaseError.getCode());
}
});
Hope it helps
happy coding

how to use firebase query in if else condition

here is the code of the onclick of button , what i want to do is on click of his button app must fetch the email from the firebase database if successful it must show up in edit text that email found else show in Edit-text that email not found , here i am able to fetch the email and show that email found on Edit-text but not able to show the email not found (else part of the code ) instead i get this in console
W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "email"' at /users/users to your security and Firebase rules for better performance
conbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Firebase ref = new Firebase("https://(refrence of firebse database)");
final String searchEmail = emailcon.getText().toString().trim();
final Query query = ref.orderByChild("email").equalTo(searchEmail);
query.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot child : dataSnapshot.getChildren())
{
final String fetchEmail;
Map<?, ?> value = (Map<?, ?>) child.getValue();
Log.d("main2activity ","User data : "+ value);
fetchEmail = (String) value.get("email");
Log.d("main2activity ","User email : "+ fetchEmail);
if (searchEmail.equals(fetchEmail))
{
emailcon.setText("email found hurray "+query.getRef());
}
else
{
emailcon.setText("still no email found!!");
}
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
});
Since you're firing a query, you will get a snapshot that can contain 0 or more children. If it contains any children, those children will have the email address you used in equalTo()
You need to handle the onDataChange() slightly differently:
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
emailcon.setText("email "+searchEmail+" found at URL "+child.getRef());
}
}
else {
emailcon.setText("still no email found!!");
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});

Categories

Resources