How to fetch data from firebase to recyclerview - android

I have an app, where I try to implement a function that would allow to create posts like in facebook. So, I want to create recyclerview that would fetch a data and display it on the screen when the post is created.
How do I do this?
I've written a code for this. But I get an error that in EventRecyclerAdapter.java in 40th line of code .get(position) is called on null reference object.
https://github.com/garaanon/IACS Here you can see the .java file I am referencing

Your Else Condition will trigger when the Data is Null than why are you trying to get Description When it is Null.Remove the getDesc() from Else Condition will Remove Error.

Related

Store edittext into MutableList

I am trying to create a function that store 3 editext values into an object when a button is pressed. These objects are stored into an mutablelist, which will be returned and displayed in a recycleview.
fun getWish(): MutableList<Wish>{
val wishes : MutableList<Wish> = ArrayList()
add_button.setOnClickListener {
val name= name_input.getText().toString()
val price= price_input.getText().toString()
val url= url_input.getText().toString()
val wishTemp = Wish(name, price, url)
wishes.add(wishTemp)
print(wishTemp)
print(wishes)
}
return wishes
}
The issue is the list is not displaying in the recycleview. The recycle view adapter is not the issue because it was tested with a test list. Is my implementation not working?
Print function shows reference values not the actual string. However, the 'wishes' list is added to everytime the button is pressed.
Where are you displaying the new data in the RecyclerView? All you're doing here is creating a new list and adding some Wish objects to it. You need to add this stuff to your data set in the Adapter, and then call one of the notify* methods to tell it to update the display (like notifyDataSetChanged). You haven't shown where you're doing that, so there's no way for us to know if that implementation is working or not.
(You probably want to show your adapter code anyway, there's a difference between initialising it with some data, and updating it with some other data later)
You're getting references with print because, at a guess, Wish is a plain class and you haven't overridden the toString() method to make it print your strings nicely. If you can make it a data class that will give you a default implementation that prints the values of your constructor parameters.

NullPointerException when writing to Firebase database

I am trying to add an item to a user in my Firebase database but I keep getting a NullPointerException every time I try and access the location I want to write the data. This location does not yet exist (because I need to first write data to it); however, this was not a problem when adding a "userID" section and adding information underneath it, like so:
database = FirebaseDatabase.getInstance();
userReference = database.getReference("accounts");
DatabaseReference currentUserDB = userReference.child(user.getUid());
currentUserDB.child("name").setValue(name);
The above section of the code adds a key with the user ID and then adds the users name inside it. This is similar to what I want to do next.
What I would like to do is add a section called "item" and inside that add "first" with the value name. Like this .
Where the keys "item" and "first" don't exist until the user first clicks the button to run this code. This code should also work item "second" and so on.
database = FirebaseDatabase.getInstance();
DatabaseReference dbR = database.getReference("accounts").child(uid);
reference = dbR.child("item");
DatabaseReference databaseReference = reference.child("test");
databaseReference.child("name").setValue(name);
I get a NullPointerException at this line:
DatabaseReference databaseReference = reference.child("test");
and I'm not sure why.
I am sure this has been answered in some form because it seems so simple, but I haven't seen anywhere that discusses writing two keys to the database at once.
Any help appreciated. My apologies if this is a duplicate.
Cheers
Edit: I know what a NullPointerException is, i just don't know why I'm getting one in this instance. The two sets of code are very similar and I'm not sure why the second code set gives me an error.
You should write your code like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("accounts")
.child("test")
.child("item")
.child("first")
.child("name")
.setValue(name);
If possible put your code here, it would help.
I think if "item" and "first" doesnt exist you can't use it as reference cause the node isnt there.
You need to use reference = dbR.setValue("item") first, and then you can use use as ref.

Firebase Android addChildEventListener explanation

I need some clear explanation with addChildEventListener and Query event binding.
I use firebase in my android app. In the onCreate() method of my activity, I create something like this...
fiDbQuery = fiDbRef.child("users").orderByChild("name");
fiDbQuery.addChildEventListener(// listener //);
... where fiDbQuery is a Query instance in the activity (global var) and fiDbRef is a DatabaseReference instance in the activity too, where I already set the initial value.
all is well, when activity first loaded, all the users data are loaded and shown. But then, I have some button to sort this data according to users field, either by email, age, name, etc.
and in those buttons onClickevent listener, I do something like this... (this one is the sort by age button)
fiDbQuery = fiDbRef.child("users").orderByChild("age");
... and I didn't provide the addChildEventListener function because it is already "added" on onCreate() method, right? The data aren't loaded, but if I add the addChildEventListener function it works again.
My question is, is it save to use multiple addChildEventListener into one Query, or is there any performance issues with that? All I want to accomplice is reusing the fiDbQuery instance without the need to add addChildEventListener every single time I modify the fiDbQuery.
Thank You for your help.
EDIT:
all those query I make use the exact same listener. Only one listener for all those query which populate a listView.
You don't need to add the addChildEventListener again. What you need to do, is to change the logic of your actions a little bit.
First define all your queries like this:
nameQuery = fiDbRef.child("users").orderByChild("name");
ageQuery = fiDbRef.child("users").orderByChild("age");
//and so on
To solve your problem, you only need to use an if else-if statement which sounds like this:
Query query;
if(nameButton.isClicked()) {
query = nameQuery;
} else if(ageButton.isClicked()) {
query = ageQuery;
}
query.addChildEventListener( // listener. //);
This means that according on which button the click was made, you are using the desired query. With this code, you just use the addChildEventListener once.
Hope it helps.

Going to parent of an entry in firebase database

I am attaching a picture of my firebase database. I am at OPL and I want to go to its parent. Is there somekind of "GetParent()" kind of fuction, like "GetChildren" that can take me there?
To read the data from database, you need to set listeners at appropriate locations. These listeners provide values for one time as well as for updates with the methods onChildAdded() and onChildChanged().
For your case, you should attach a childListener on the ActionList by using the line
mDatabase.child('ActionList').addChildEventListener(listenerName);
Set up the onChildAdded() method of this listener which provides a snapshot. Traversing through the children from that snapshot using dataSnapshot.getChildren() and to get their values use this function child.getKey() method.

how to add firstvalue by default null in spinner.?

I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.

Categories

Resources