I know this has been asked a lot,but i'm really sorry,i am a noob in this,i'll just attach the code,here
l =adminUserlst.split(",");
UsrList = new ArrayList<String>();
for(i=0;i<=l.length-1;i++){
DatabaseReference db33 =firebaseDatabase.getReference("USERS/");
db33.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
***String spl=dataSnapshot.child(l[i]).child("sList").getValue().toString();//.split(",");
splits =spl.split(",");
for(ii=0;ii<=splits.length-1;ii++){
UsrList.clear();
UsrList.add(splits[ii]);
usrAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, UsrList);
spinner.setAdapter(usrAdapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I have some references in l,by which i want to extract some data from firebase database,so once i split the data,i'm using the split data present as a child node to get some more information,but for some reason i'm getting a
ArrayIndexOutofBounds:length=1;index=1 at ***
Any help would be appreciated
check with writing your for loop condition in bracket
like this
for(i=0;i<=(l.length-1);i++)
on both places
Related
im working with firebase database. i want to get specific data after searching from "data" node. Below im sharing my database Screenshot and java function that i have already tried.
I'm working on it from two days. but i can't solve it.
please help me.
Thanks in advance.
private void getSearchedData(String selectedItem) {
reference = FirebaseDatabase.getInstance().getReference().child("Category");
Query query = reference.orderByChild("data/dataName").equalTo(selectedItem);
ArrayList<ChannelData> channelDataArrayList = new ArrayList<>();
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot requestSnapshot : snapshot.getChildren()) {
DataSnapshot channelsSnapshot = requestSnapshot.child("data");
for (DataSnapshot ds : channelsSnapshot.getChildren()) {
ChannelData channelData = new ChannelData(ds.child("dataName").getValue(String.class),
ds.child("dataImage").getValue(String.class));
channelDataArrayList.add(channelData);
searchAdapter adapter = new searchAdapter(channelDataArrayList, getApplicationContext());
rvSearchedChannel.setAdapter(adapter);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Toast.makeText(MainActivity2.this, "Error: " + error, Toast.LENGTH_SHORT).show();
}
});
}
And this is my database structure. i want to get "dataImage", "dataName" and "dataLink" after searching "dataName".
if you are using edit text for as a search box. add addTextChangedListener on edit text and you will get 3 methods on afterTextChanged method creat a function and pass it editable.tostring you get it in function.
and function code is :
FirebaseDatabase.getInstance().getReference().child("Category").child("data/dataName").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren())
{
list.clear();
for (DataSnapshot dss : dataSnapshot.getChildren())
{
Register register = dss.getValue(Register.class);
if (register.getUsername().toLowerCase().contains(s.toLowerCase())){
list.add(register);
}
if (Objects.equals(register.getId(), FirebaseAuth.getInstance().getCurrentUser().getUid())){
list.remove(register);
}
}
SearchUserAdapter adapter = new SearchUserAdapter(list, getContext());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
search_rv.setLayoutManager(linearLayoutManager);
search_rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Queries in the Firebase Realtime Database search the direct child nodes of the path you run them at for a value at a fixed path that you specify.
In your example, the path of the value in the JSON is not fixed as you have relevant nodes in data/01/dataName and data/02/dataName, which isn't possible. Since the path that you order/filter on (data/dataName) doesn't even exist under Category/01, so you won't get any results.
If you want to allow this query on the database, you'll need to change the data structure (or add an additional one) where the values you want to order/filter on are at a fixed path under the direct child nodes of the path you query. So you'll have to lose a level of your JSON structure, but can keep the information by for example duplicating the categoryName value into each child node:
CategoryData: {
"0101": {
CategoryName: "Pakistani",
dataName: "News",
...
},
"0102": {
CategoryName: "Pakistani",
dataName: "Dramas",
...
},
"0201": {
CategoryName: "India",
dataName: "news",
...
},
"0202": {
CategoryName: "India",
dataName: "movies",
...
}
}
I am trying to populate a spinner with values from my firebase database.
fdatabase = database.getReference();
fdatabase.child("SPORTS").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> Sports = new ArrayList<String>();
for (DataSnapshot snapshot:dataSnapshot.getChildren()) {
String SportName = dataSnapshot.child("SportName").getValue(String.class);
Sports.add(SportName);
}
Spinner spinner = (Spinner) findViewById(R.id.SportSpinner1);
ArrayAdapter<String> SportAdapter = new ArrayAdapter<String>(RegComplete.this, android.R.layout.simple_spinner_item, Sports);
SportAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(SportAdapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here is the code and i'll share a snapshot of my database as well. Please help.
I tried your peace of code and modified the child names to get a value of my own database and it worked, so my guess is, that your code does not fit to your data structure.
But it's hard to help you, if you don't provide us your data structure. You try to get the value of
yourDatabase/"SPORTS"/"Sportname"
But I bet, you are missing a key in between and you actually meant this:
yourDatabase/"SPORTS"/SOME MISSING KEY/"Sportname"
So, try to changed this line
String SportName = dataSnapshot.child("SportName").getValue(String.class);
into this:
String SportName = dataSnapshot.child(**MISSING KEY**).child("SportName").getValue(String.class);
I'm a beginner user of android studio.I need your help in this issue.
I wrote a code to save strings in firebase database, and I want to write a code that retrieve all the values under a certain key from multiple nodes and display it in a listview.
How can I do so ?
data structure:
DataBase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> a = new ArrayList<>();
a.add(dataSnapshot.child("imageURL").getValue().toString());
a.add(dataSnapshot.child("n1").getValue().toString());
a.add(dataSnapshot.child("n2").getValue().toString());
a.add(dataSnapshot.child("n3").getValue().toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
and after that set this list to adapter using setAdapter ..
I hope this code will help you :)
I have this data in Firebase Database:
Negocios:
-KjfCu56lFZCybYldBZy
lugarReference: "Restaurante"
numeroMesas:39
password:"pass123"
user:"user123"
user_id:"usr_1"
I have two applications connected to one project in Firebase, one of them upload data of restaurants reservation, and the other one displays reservations in a RecyclerView.
In the first application, once I do a reservation, it must minus one to numeroMesas value, the other application should display any changes to this key.
In the reservation application, I have this piece of code that I did base in this question:
Update specific keys using Firebase for Android
It supposes to modify the value of numeroMesas, but it does not:
mReference.child("Negocios").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Lugar lugar = postSnapshot.getValue(Lugar.class);
if(nombre_lugar.equals(lugar.getUser_id())){
DatabaseReference numMesasReference = mReference.child(dataSnapshot.getKey()).child("numeroMesas");
numMesasReference.setValue(lugar.getNumeroMesas() - 1);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
What it does is the next thing:
Negocios:
-KjfCu56lFZCybYldBZy
lugarReference: "Restaurante"
numeroMesas:39
password:"pass123"
user:"user123"
user_id:"usr_1"
-Negocios
-KjfCu56lFZCybYldBZy
numeroMesas:39
How can be fixed?
I need to minus one once I do a reservation...
Greetings!
Using postSnapshot.getRef() you obtain a reference to the source location for this snapshot. So what's next is to add a child to that reference, that reference will be "numeroMesas" that is the key you want to modify.
As easy as that, the code will be like this.
mReference.child("Negocios").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Lugar lugar = postSnapshot.getValue(Lugar.class);
if(nombre_lugar.equals(lugar.getUser_id())){
DatabaseReference numMesasReference = postSnapshot.getRef().child("numeroMesas");
numMesasReference.setValue(lugar.getNumeroMesas() - 1);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
P.S: If you want to minus one just once by reservation, addValueEventListener() won't work, it will minus multiple time as a loop. Use addListenerForSingleValueEvent() instead addValueEventListener().
As the name says, this listener fits for single changes in the data at any location.
I guess the problem is with dataSnapshot.getKey() in this line
DatabaseReference numMesasReference = mReference.child(dataSnapshot.getKey()).child("numeroMesas");
Here you getting the key for the whole list of Negocios, not for the specific child replace it with `
postSnapshot.getKey()
The problem is that your DatabaseReference that you are using is wrong. You are missing a child. So in order to solve this problem, please use the following code:
mReference.child("Negocios").child(negociosId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int numeroMesas = (int) dataSnapshot.child("numeroMesas").getValue();
if(nombre_lugar.equals(lugar.getUser_id())){
mReference.child("Negocios").child(negociosId).child("numeroMesas").setValue(numeroMesas - 1);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
In which negociosId is the unique id generated by the push() method.
Hope it helps.
I'm new to firebase and android. But If the above solutions doesn't work please do the following. I think the problem is that you didn't convert the value of numeroMesas to string. Sorry if there's anything wrong.
Hope it helps.
mReference.child("Negocios").child(negociosId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(nombre_lugar.equals(lugar.getUser_id())){
mReference.child("Negocios").child(negociosId).child("numeroMesas").setValue((Integer.parseInt(dataSnapshot.child("numeroMesas").getValue().toString())) - 1);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
In my Android app I am using this code to retrieve posts from firebase database:
ArrayList<Post> posts = new ArrayList<>();
dbRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dsp : dataSnapshot){
String author = dsp.child("author").getValue().toString();
String text = dsp.child("author").getValue().toString();
posts.add(new Post(author,text));
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this returns all the posts and if there would be many of them I think it wouldn't be good idea to keep them all in memory. Is there a way to get only certain number of node's children? For example at the beginning I would like to get 10 first ones, then 10 more and so on
You're looking for limitToFirst() (or limitToLast()).
ArrayList<Post> posts = new ArrayList<>();
Query query = dbRef.orderByKey().limitToFirst(10);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dsp : dataSnapshot){
String author = dsp.child("author").getValue().toString();
String text = dsp.child("author").getValue().toString();
posts.add(new Post(author,text));
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
See the Firebase documentation on queries for an explanation of all its capabilities.
You need to use your DataSnapShot to retrieve more specific information.
There is a Firebase guide for Android that explain how to utilize: Firebase Android Guide
If you would prefer to see on Stack OverFlow a small example this answer here seems to be doing somewhat of what you would like to achieve: Firebase Get Children