I am querying on firebase to fetch data from last point which was previously fetched but it is always giving all the rows.
Code:
Query query = null;
query = mFirebaseDatabaseReference.orderByKey();
String lastKey = getLastKey();
if (!TextUtils.isEmpty(lastKey)) {
query.startAt(lastKey);
}
Please help
DatabaseReference databaseReference = Firebase.getInstance().getReference();
Query lastQuery = databaseReference.child("NODE_NAME").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// you can get last node values..
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});
// where NODE_NAME is the root of all these child keys.
Related
I'am trying to store the query result in a String variable, but the result I'm getting is the string conversion of the query object.
tableDatabaseReference = FirebaseDatabase.getInstance().getReference("tables");
tableNameQuery = tableDatabaseReference.child("tableName").orderByChild(tableID).equalTo("123");
String tableName = tableNameQuery.toString();
Any suggestions?
EDIT:
Database structure:
Tables:
-------TableID:
--------------TableName:
--------------TableID:
To retrieve data from Firebase Realtime database , you have to add a valueEventListener to your database reference variable like this
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dataRef = database.getReference("YOUR_DATABASE_REF");
dataRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here you can get your data from this snapshot object
String data = dataSnapshot.getValue("CHILD_NAME_HERE").toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To get data once you can use singleValueEventListener
dataRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here you can get your data from this snapshot object
String data = dataSnapshot.getValue("CHILD_NAME_HERE").toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
Firebase operation is asynchronous, so you have to wait to complete the operation to get result. To access data outside of onDataChange you can use LiveData and observe it outside. When you get data inside onDataChange then notify LiveData by posting value. Check below:
private MutableLiveData<String> tableName = new MutableLiveData<>();
DatabaseReference tableDatabaseReference = FirebaseDatabase.getInstance().getReference("tables");
tableDatabaseReference
.child(tableID)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("tableName").getValue(String.class);
//Now post that value to LiveData
tableName.postValue(data);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Observe data here
tableName.observe(this, name -> {
// use name here
});
I have developed an admin side application from where I will be adding the email id, pin, city and country data to the firebase realtime database. Then I will be providing the email and pin to the customer. So my simple target is that when the customer logins, the data of city and country should be displayed on his application. To do this I have to search the database for that particular customer's node which can be done through the email id which he puts during login.
I tried my best but everytime null is returned. Please have a look at my code and help me out.
void searchQuery(String email){
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("arvi-admin").orderByChild("mailId").equalTo(email);
query.addValueEventListener(valueEventListener);
}
ValueEventListener valueEventListener = new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
try {
DataModel obj = dataSnapshot.getValue(DataModel.class);
logData(""+obj.getCity()); //the values are null
}catch (Exception e){
Log.d("##","Error");
e.printStackTrace();
}
}
#Override
public void onCancelled(DatabaseError databaseError){
}
};
Firebase Database Image
To solve this, please change the following line of code:
Query query = mFirebaseDatabaseReference.child("arvi-admin").orderByChild("mailId").equalTo(email);
to
Query query = mFirebaseDatabaseReference.orderByChild("mailId").equalTo(email);
As you can see, I have removed the call to .child("arvi-admin") because the root is not a child and should not be added in your reference.
Edit:
Query query = mFirebaseDatabaseReference.orderByChild("mailId").equalTo(email);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
DataModel obj = ds.getValue(DataModel.class);
Log.d(TAG, obj.getCity());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
I am setting the value of a child in Firebase, but when I get the value from Firebase the value is null. I don't understand why.
database.child("User").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int depositInteger = 0;
try {
depositInteger = Integer.parseInt(depositText.getText().toString().trim());
} catch (NumberFormatException e) {
if (depositText.equals("")) {
Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
}
}
database.child(user.getDisplayName()).child("deposit").setValue(depositInteger);
Long previousDeposit = dataSnapshot.child("User").child(user.getDisplayName()).child("deposit").getValue(Long.class);
System.out.println("VAAAALUE: " + previousDeposit);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To set a value, you can use setValue() method directly on the reference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users").child(user.getDisplayName());
userRef.child("deposit").setValue(9); //Sets the value to 9
To get the value, please use the following code:
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int deposit = dataSnapshot.child("deposit").getValue(Integer.class);
Log.d("TAG", deposit);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);
Please see the correct DatabaseReference which contains Users and not only User as shown in your screen-shot.
According your database structure the child you should reference is Users and in your code you are calling User. Fix that mistake...
//Replace child tag by Users to fix the error
database.child("Users").addValueEventListener(new ValueEventListener() { ...
I am new to firebase. I want to search for some email ids using orderbychild() query. Please help me, I have posted my code below. This code returns value empty.
private void getFilterDataByEmail(String s) {
mSearchProgress.setVisibility(View.VISIBLE);
userLists.clear();
final DatabaseReference usersReference = FirebaseDatabase.getInstance().getReference();
Query query;// = usersReference.child(ARG_USERS).orderByChild("username").startAt(s.toString());
query = usersReference.child(ARG_USERS).orderByChild("email")
.startAt(s.toString()).endAt(s + "\uf8ff");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
doTask(dataSnapshot, true);
}
#Override
public void onCancelled(DatabaseError databaseError) {
mSearchProgress.setVisibility(View.INVISIBLE);
suggestionListLayout.setVisibility(View.GONE);
}
});
}
The response I have got is:
DataSnapshot { key = users, value = null }
Basically I have to listen a object. Please see the attached images.
Memo List
Memo Object
So I need to get all memos which equal to hash_tag = "love". How to overcome this problem?
Query query = memo.child(Memo.HASH_TAG).startAt("love");
query.addChildEventListener(mMemoChildEventListener);
You can check some basics guides for retrieving data here https://firebase.google.com/docs/database/admin/retrieve-data
Android - How to query in firebase
MemoObject memoobject=new MemoObject();
ArrayList<MemoObject> memoobjectArrayList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(Memo).orderByChild("hash_tag").equalTo("love");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot dataSnapshots :dataSnapshot.getChildren()) {
memoobject=dataSnapshots.getValue(MemoObject.class);
for(int i=0;i<memoobject.getHashTag().size();i++){
if(memoobject.getHashTag().get(i).equals("love"){
memoobjectArrayList.add(memoobject);
}
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});