Firebase query result in ListView UI and RecyclerView UI - android

I am trying to create a Firebase database to store employee information and then when an address is queried, all the employees staying in that particular area should be displayed in a ListView or a RecyclerView however, I am having the following problem:
I am unable to populate the ListView.
ArrayList<String> mItems = new ArrayList<>();
ArrayAdapter mAdapter;
ListView mRecyclerView;
FirebaseListAdapter<String> fireAdapter = new FirebaseListAdapter<String>() {
#Override
protected void populateView(View view, String s) {
//populate view
}
};
In the above code, I am unable to write the constructor so that it becomes,
FirebaseListAdapter<String> fireAdapter = new FirebaseListAdapter<String>(this,String.class,
android.R.layout.simple_list_item_1,mEmployRef) {
#Override
protected void populateView(View view, String s) {
//populate view
}
};
When I write the above code with the constructor, it says,
Cannot resolve constructor 'FirebaseListAdapter(anonymous android.view.View.OnClickListener, java.lang.Class, int, com.google.firebase.database.DatabaseReference)'
How do I resolve this? A similar problem occurs when I try to use a RecyclerView.

You're creating the adapter inside an OnClickListener(), which means that this points to that listener. You need to pass in the Activity to FirebaseListAdapter, which you can get with MainActivity.this.

I was having this problem when Firebase upgraded and what i did to get out of the constructor error was change this:
Firebase rootRef = new Firebase("https://<your-app>.firebaseio.com/");
to this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
then use the rootRef as the fourth parameter on the constructor.
here is a link for upgrading from the previous firebase to the current:
https://firebase.google.com/support/guides/firebase-android#import_your_project_to_the_new_firebase_console_numbered
Also, upgrade your sdk. Hope this solves your problem

Related

Can i get documents from collecions i have on Firestore

I have my fire store set up and connected to my app. There are these categories and I want when I click on one category to fetch all subcategory from that category. Can someone explain to me step by step how to do that?
public class BeautyIzbornik extends Activity {
ImageButton imageButton;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference firemRef = db.collection("beauty");
private FirmeAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firmeinfo);
setUpRecyclerView();
}
private void setUpRecyclerView(){
Query query= firemRef.orderBy("logo",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Firme> options = new FirestoreRecyclerOptions.Builder<Firme>()
.setQuery(query,Firme.class)
.build();
adapter= new FirmeAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view2);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
my Fire store looks like this
This is my main category and subcategories:
This are subcollection from one subcategory:
And now in my Main activity, I open the main category and I want when I press on subcategory(whic are all displeyed in my main activity) I want to open new activity and fill it with items from that subcategory... I have more subcategories and items but I hope you understand what I want to show.
so i want to have my Main activity filled with categoriys,secnd activity to fill with subcategorys and third activity where i will display data of some items that is clicked in secnd activity i hope you understand me my englis is bad :D
It would be more useful if you provided the database schema. But the idea is the same on all schemas.
Query query= firemRef.document("myDocumentHERE");
...
You can get the collection of the "myDocumenthere" by doing the following
Query query= firemRef.document("myDocumentHERE").collection("myCollection");
I'd recommend reading the official documentation, there's code examples there and its explained much better.
Source
Can i get documents from collecions i have on Firestore
yes you can! do you want to display your data in recycler?
Try this. Watch Now
to retrieve your beauty in document table, please use this:
firebaseFirestore...
String postId = doc.getDocument().getId(); //retrieving 'beauty` in document table
MyContent myContent = doc.getDocument().toObject(MyContent.class.withId(postId));
in Adapter class you will retrieve it again using this:
final String postId = contentList.get(position).PostId;
firebaseFirestore.collection("kategorije").document(postId).collection("beauty").addSnapshotListener(new EventListener<QuerySnapshot>() ...
please see his videos for details.

Recycler View Retrieving Childs of different Nodes

[
I am planning to retrieve data for a recycler view from the node "movies" and "tickets". I am using Android Studio.
Till now I have a code to retrieve data just from the node "movies".
Is there a possibility to add something to the code to be able to retrieve data from "tickets" also in the same RecycleView?
Thanks for your help :)
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
// send query to firebaseDatabase
mFirebaseDatabase = FirebaseDatabase.getInstance();
// the following line i suppose needs to change to "Cities"
mRef = mFirebaseDatabase.getReference().child("Cities/movies");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Movie, MovieViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Movie, MovieViewHolder>(
Movie.class,
R.layout.movies_all,
MovieViewHolder.class,
mRef
){
#Override
protected void populateViewHolder(MovieViewHolder viewHolder, Movie movie, int position){
viewHolder.setDetails(getApplicationContext(),
// the following line(s) again needs to change, but I don't know to what
movie.getTickets(),
movie.getKino(),
movie.getTime();
}
Edit: Or do I need a second "ViewHolder.class" and a second "FirebaseRecyclerAdapter" to retrieve from different nodes?

FirebaseListAdapter - Query

Is it possible to specify the which data is read by the FirebaseListAdapter?
At the moment, I could only read the complete database. The database holds a lot of different objects. The object has a value which represents the User who has created the object.
Now, I only want to read the objects from this special user. When I try it like this, there are empty list field.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("URL");
//HOW CAN I DO THE QUERY FOR THAT DATABASE REFERENCE?
FirebaseListAdapter<Machine> firebaseListAdapter = new FirebaseListAdapter<Machine>(
this,
Machine.class,
R.layout.list_item,
databaseReference) {
#Override
protected void populateView(View v, final Machine model, int position) {
//CAN I DO THIS BY QUERY?
if(model.getS_UserID.equals(user.getID)){
//SOME STUFF WHICH I DO
}
};
}
list.setAdapter(firebaseListAdapter);
The question is, can I do the If-Statement by doing a query for the FirebaseListAdapter?
At the moment, I do something like this:
if(model.getS_UserID.equals(user.getID()) --> DO SOME STUFF
But it also creates some empty fields which are really ugly.
My Database looks like this:
OBJECT
-USERID = STRING
-NAME = ...
-.....
You can pass in a Query to the FirebaseListAdapter instead of the database reference you use right now. To filter on items with the user ID:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("URL");
Query query = databaseReference.orderByChild("s_UserId").equalTo(user.getID);
FirebaseListAdapter<Machine> firebaseListAdapter = new FirebaseListAdapter<Machine>(
this,
Machine.class,
R.layout.list_item,
query) {
Read more about this in the Firebase documentation on ordering and filtering data.

Why Firebase DB is jumping one level deeper?

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.

Filter results in FirebaseRecyclerAdapter

I am new to Android and just started to understand some concepts like the RecyclerView. I am using Firebase as a database so I implemented the Firebase solution for that.
My Adapter:
FirebaseRecyclerAdapter<Offer,OfferViewHolder> adapter = new FirebaseRecyclerAdapter<Offer, OfferViewHolder>(
Offer.class,
R.layout.card_item,
OfferViewHolder.class,
mDatabaseReference.child("offer").getRef()
) {
#Override
protected void populateViewHolder(OfferViewHolder viewHolder, Offer model, int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvHeading.setText(model.getHeader());
viewHolder.tvStoreName.setText(model.getStoreName());
}
};
Now I have two questions:
Is it possible to filter results inside the adapter or do i have to use ChildEventListeners for that?
When referencing to a key that contains an Array or an object itself how to retrieve child values?
You can initialize the FirebaseRecyclerAdapter with either a DatabaseReference or a Query. As its name implies, the latter allows you to get a subset of the children at a specific location in the database.
For example say if you want to only show offers from a specific store:
DatabaseReference offers = mDatabaseReference.child("offer").getRef();
Query storeOffers = offers.orderByChild("storeName").equalTo("A. lazzi's store");
FirebaseRecyclerAdapter<Offer,OfferViewHolder> adapter = new FirebaseRecyclerAdapter<Offer, OfferViewHolder>(
Offer.class,
R.layout.card_item,
OfferViewHolder.class,
storeOffers
) {

Categories

Resources