I am having trouble retrieving a List from the Firebase. I have no trouble storing it, but as soon as I try to cast dataSnapshot.getValue() to ArrayList my app crashes, giving an exception:
HashMap cannot be casted to ArrayList
But when I tried to cast it to a HashMap, it also crashes, giving exception:
ArrayList can't be casted to hashmap
Need help please! Here is the code that is creating the problem:
Fire.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { List<TaskDes> td = (ArrayList<TaskDes>) dataSnapshot.getValue() notifyDataSetChanged(); } @Override public void onCancelled(FirebaseError firebaseError) { } });
I want to retrieve all the data in the Firebase as one List. The class TaskDes contains three fields:
class TaskDes { // definition boolean done String taskDescription String taskTitle }
Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.
You do that like this: // Get a reference to your user final FirebaseDatabase database = FirebaseDatabase. getInstance(); DatabaseReference ref = database. getReference("server/path/to/profile"); // Attach a listener to read the data at your profile reference ref.
Firebase Realtime Database provides us a feature to give Real-time updates to your data inside your app within milli-seconds. With the help of Firebase, you can provide Real-time updates to your users. In this article, we will take a look at the implementation of the Firebase Realtime Database for our ListView in Android.
To read and write data from the database, you need an instance of DatabaseReference: // ... // ... Use the push () method to append data to a list in multiuser applications. The push () method generates a unique key every time a new child is added to the specified Firebase reference.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. After creating a new project. Navigate to the Tools option on the top bar. Inside that click on Firebase.
as Frank said Firebase stores sequence of values in the format of "key": "Value" which is a Map structure initialize GenericTypeIndicator with HashMap of String and your Object. get value of DataSnapShot as GenericTypeIndicator into Map. initialize ArrayList with HashMap values.
You need to create a GenericTypeIndicator object to pass as DataSnapshot.getValue()
parameter.
Code:
GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {}; List<String> yourStringArray = dataSnapshot.getValue(t);
Your Model
public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } }
You need to create a GenericTypeIndicator object to pass as DataSnapshot.getValue()
parameter.
In Activity
private static final String TAG=MainActivity.class.getSimpleName(); private FirebaseDatabase database; private DatabaseReference myRef=null; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database=FirebaseDatabase.getInstance(); myRef=database.getReference("ADD_YOUR_REFERECE"); myRef.addValueEventListener(new ValueEventListener(){ @Override public void onDataChange(DataSnapshot dataSnapshot){ /* This method is called once with the initial value and again whenever data at this location is updated.*/ long value=dataSnapshot.getChildrenCount(); Log.d(TAG,"no of children: "+value); GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); for(int i=0;i<taskDesList.size();i++){ Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); } } @Override public void onCancelled(DatabaseError error){ // Failed to read value Log.w(TAG,"Failed to read value.",error.toException()); } }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With