the situation is following, I'm using Firebase DB for a small application, the structure of DB is the following.
I'm aware this kind of structuring is not the best for Firebase, probably the problem is here.
My question is, when I'm trying to get childrens list from a parent, Firebase provides me with childrens list from one level deeper entity. For ex. related to this DB sample - if I try to get childrens list from the "University" entity, I get the output "Faculty", but should get the output "UNN". Am I right?
Here is the code sample I'm using for retreiving data:
mDBRef = FirebaseDatabase.getInstance().getReference().child("University");
final ListView listView = (ListView) findViewById(R.id.lv_main);
mGruppeChildEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
List<String> lst = new ArrayList<String>(); // Result will be holded Here
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
lst.add(String.valueOf(dsp.getKey())); //add result into array list
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lst);
listView.setAdapter(adapter);
}
Well it is because of firebase's onChildAdded method come with parameter of children of the parent you are reading from. If you added child event listener on 'University', you will get value like 'UNN'-->Faculty. Again if you perform getChildren method on this, you will get 'Faculty'-->RGF. So here if you apply getKey, off course it will return Faculty.
Related
Here is my problem, I want to show all phone numbers in RecyclerView, how?
databaseReference = firebaseDatabase.getReference("CallLog");
//in on data change method using for
CallLogList logList = snapshot.getValue(CallLogList.class);
list.add(logList);
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String, String>(); map.put("phone",phoneNumber);
map.put("name",callName);
map.put("callType",dir);
map.put("date",dateString);
map.put("duration",callDuration+" seconds");
arrayList.add(map);
myRef.push().setValue(arrayList);
After getting the whole list from Firebase iterate them one by one to add them in the list, after the loop set them in the adapter of RecyclerView, and set the adapter to your desired RecyclerView
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
list = new ArrayList<CallLogList>();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
CallLogList logList = snapshot.getValue(CallLogList.class);
list.add(logList);
}
yourAdapter = YourAdapter(list);
yourRecyclerView.setAdapter(yourAdapter);
}
For more info, here is a full example
Note: If you are using LiveData or Paging then setting adapters is different, Let me know if you need more example
First you should change the tree to be like this:
Call Log
-Calls
--Lhe...
---CallType OUTGOING
--Lhe...
---CallType INCOMING
Next, create "Calls" class in your project with the same fields as in the firebase, so callType, date, duration and phone.
After that, with this code, you can get all instances from the database into a list:
public void refreshList(DataSnapshot dataSnapshot) {
List<Class> list = new ArrayList<>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.child("Calls").getChildren())
{
Item value = dataSnapshot1.getValue(Calls.class);
list.add(value);
}
}
Now you have a list of objects of class Calls, from which you can easily access the phone numbers.
I require help in implementing data from a secondary Firebase database into autocomplete TextViews in my android application. I have a database containing data on cars (Make of car, model of car, engine size).
I have attached an image displaying how my data is set in the database.
Database image
I would like to have one TextView that will only display all the "make" attributes, another TextView to display only "model" attributes depending on which "make of car" the user picked. (eg. if the user picks Audi, models displayed would be A4, A6, A3 only. if the user picks Volkswagen, models displayed would be Golf, Jetta, Passat only) and then the final TextView to display only the "engine size" that corresponds to the chosen "make" and "model" as each model of car has different engine sizes.
Any help would be greatly appreciated as I have been stuck on this element of my app for several weeks now and have tried countless different approaches.
Below is my class and code. The application runs with no errors but will not retrieve the data from the database.
public class VehicleSpec extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Sets the layout according to the XML file
setContentView(R.layout.activity_vehicle_spec);
//FirebaseDatabase database = FirebaseDatabase.getInstance();
//
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("com.example.user.vel")
.setApiKey("AIzaSyBvBtL81H7aiUK90c3QfVccoU1CowKrmAA")
.setDatabaseUrl("https://finalyearproject-vel1-aac42.firebaseio.com/")
.build();
//
FirebaseApp.initializeApp(this, options, "secondary");
//
FirebaseApp app = FirebaseApp.getInstance("secondary");
FirebaseDatabase database2 = FirebaseDatabase.getInstance(app);
DatabaseReference dbref = database2.getReference();
DatabaseReference dbref2 = database2.getReference();
DatabaseReference dbref3 = database2.getReference();
//
dbref.child("Cars").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<String> Cars = new ArrayList<String>();
for ( DataSnapshot suggestionSnap : dataSnapshot.getChildren() )
{
String suggestion = suggestionSnap.child("Make").getValue(String.class);
Cars.add(suggestion);
}//End For()
//XML TextView variable
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.auto);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
actv.setAdapter(adapter);
}//End onDataChange()
public void onCancelled(DatabaseError databaseError)
{
}//End onCancelled()
});//End dbref ValueEventListener()
//
dbref2.child("Cars").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<String> Cars = new ArrayList<String>();
for ( DataSnapshot suggestionSnap : dataSnapshot.getChildren() )
{
String suggestion = suggestionSnap.child("Model").getValue(String.class);
Cars.add(suggestion);
}//End for()
//XML TextView variable
AutoCompleteTextView actv1 = (AutoCompleteTextView) findViewById(R.id.auto1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
actv1.setAdapter(adapter1);
}//End onDataChange()
public void onCancelled(DatabaseError databaseError)
{
}//End onCancelled()
});//End dbref2 ValueEventListener()
//
dbref3.child("Cars").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<String> Cars = new ArrayList<String>();
for (DataSnapshot suggestionSnap : dataSnapshot.getChildren())
{
String suggestion = suggestionSnap.child("Engine Size").getValue(String.class);
Cars.add(suggestion);
}//End for()
//XML TextView variable
AutoCompleteTextView actv2 = (AutoCompleteTextView) findViewById(R.id.auto2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
actv2.setAdapter(adapter2);
}//End onDataChange()
public void onCancelled(DatabaseError databaseError)
{
}//End onCancelled()
});//End dbref3 ValueEventListener()
}//End onCreate()
I Don't really understand exactly what are you trying to achieve, but let's assume you want one of the followings:
Display a list of cars in a list with 3 sections , Make , Model and a drop down list for Engine Size:
i Suggest using RecyclerView with 2 TextViews (1 for Make, and the second for Model) and an AppCompatSpinner that will be populated from the Engine Size node. You can use these Libraries as your adapter :
FirebaseUI
Infinite-Fire i personaly like this one because it has a built in loadMore when you reach last the item.
Display a list of car Brands (Make in this case) and when a user selects an item, an other list with Models and Engine Sizes Appears : i Suggest using RecyclerView with A TextViews for Make and when the user clicks an item, you save the item name in your SharedPrefrances and open a new activity that displays only models from the selected Make using Firebase databaseRef.child("Cars").orderByChild("Make").equalesTo(getSelectedMake());
or just display them in an AlertDialog instead with a costume layout using the same methods...for displaying DATA You can use the above Libraries as well.
if This isn't what are you looking for , please explain your problem with details and i will update my answer.
Edit : using SharedPreferences to save a reference of the item is nonsense, it's a result of my low experience with android, instead you can use intent.putExtra()
Example
//Start your activity like this
intent.putExtra("itemId", itemId)
startService(intent)
//On your new activity's onCreate() get the itemId as the following
String itemId = intent.getStringExtra("itemId")
Item item = firebaseRef.child("cars")....getValue(Item:class)
What I want to get is "Kasarwadi" and similar node that will be added after. I have to populated spinner with this data. How can I loop over to get this?
Also when I tried to loop over in the event Listeners, foreach loop is not getting executed or I can say control is not getting transferred in foreach (enhanced for loop)loop.
code for ref`
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("people-3999c");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("STATE"," inside ondataChange");
for (DataSnapshot location : dataSnapshot.getChildren()){
Log.d("STATE","1");
locationFromFB.add(location.getKey());
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item,locationFromFB);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
}
Log.d("STATE","outside");
}`
In order to use that locationFromFB ArrayList you need to declare it inside onDataChange() method, otherwise is null, due the asynchronous behaviour.
Get also the following line ouf of the for loop because you are creating a new instance every time the for loop iterates.
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item,locationFromFB);
Please change also the DatabaseReference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Kasarwadi").child("Users");
Hope it helps.
I am currently creating an app which logs a 6-digit int id's (values) and the time/date they were logged (keys) in a real-time database. I am trying to create a separate app to accompany this by listing the database key-value as items on a list, with a search query to search the id's.
My question is, how I should go about retrieving and listing both the keys and values as items in my listView? The items need to add themselves in real-time as well, so they can be viewed as they would in the Firebase console. Thanks!
public ArrayList<String> arr;
public ArrayAdapter adapter;
#Override
protected void onStart() {
super.onStart();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
arr = new ArrayList<>();
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
map2list((Map) dataSnapshot.getValue());
//formats the datasnapshot entries to strings
adapter.notifyDataSetChanged();
//makes the ListView realtime
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
System.out.println(databaseError.toException());
// ...
}
};
mDatabase.addValueEventListener(listener);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, arr);
ListView listView = (ListView) findViewById(R.id.yourOwnListView);
listView.setAdapter(adapter);
}
public void map2list(Map<String,Long> map){
arr.clear();
for (Map.Entry<String, Long> entry : map.entrySet()) {
Long key = Long.parseLong(entry.getKey());
String d = DateFormat.getDateTimeInstance().format(key);
Long value = entry.getValue();
arr.add(d + ": " + value);
}
}
I tested this on my own database and it works.
map2list() is my quick way of formatting your data for the listview. The best way to do this is with a custom arrayadapter or baseadapter, but that involves alot more code than you need for this example.
Be advised that keeping your keys as time is a bad idea since you could have a collision if two logs are made at the same time.
Lastly, firebase uses Longs over ints, so i used Longs for less code
I'm currently using FirebaseRecyclerAdapter to retrieve data from a Firebase database, and I've pretty much worked out how to retrieve the data in the order I desire.
To avoid duplication and ease database maintenance, I'd like to add a key to a database entry that allows me to return queries based on that key. I was originally storing data twice. Once for all to see, and once if a user had joined a certain group (under groupName).
To return a query based on group, my original search was as follows:
databaseReference.child(groupName).child("exerciseId"+mExerciseId).orderByChild(sortOrder).limitToFirst(100);
but I believe duplication can be avoided by adding the key "group" to my post. (it also make maintenance much easier as users switch groups).
The "group" database query has now become:
databaseReference.child("exerciseId"+mExerciseId).orderByChild("group").equalTo(groupName);
All is good, except that the data is no longer sorted as per "sortOrder". As firebase does not allow multiple sort criteria, I believe my solution lies in offline sorting.
So, how does one sort the adapter offline?
My adapter is pretty standard:
mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post, PostViewHolder.class, dataQuery)
{
#Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position)
{
final DatabaseReference postRef = getRef(position);
// Bind Post to ViewHolder, setting OnClickListener for the star button
viewHolder.bindToPost(model, position, postRef.getKey(), null);
}
mRecycler.setAdapter(mAdapter);
}
I've implemented Comparable in Post, the problem is where is the data stored so that I can pass it to Collections.sort() in this sort of way:
private List<Post> mPosts = new ArrayList<>();
mPosts.add(model);
Collections.sort(mPosts, Post.Comparators.ALLTIME);
The adapters in the FirebaseUI library currently always display data in the order that it is returned by the underlying FirebaseDatabase reference or Query.
You could file a feature request on the Github repo for it (since I'm not sure it is covered in this one yet). Alternatively you could fork the library and roll your own implementation of this functionality.
To feedback on the solution I came up with. I ended up populating my own List, which I can then sort or add to (if using multiple queries) depending on what I'm trying to achieve. This is then displayed using a standard RecyclerView adapter. It works great.
private final List<Post> postList = new ArrayList<>();
dataQuery.addListenerForSingleValueEvent(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
postList.clear(); // in this example it's a newQuery, so clear postList
for (DataSnapshot child : dataSnapshot.getChildren())
{
Post post = child.getValue(Post.class);
postList.add(post);
int myPosition = sortData(); // uses Collections.sort() to sort data
mAdapter.addPosts(postList);
mAdapter.notifyDataSetChanged();
mRecycler.scrollToPosition(myPosition);
}
}
});