Reputation: 2211
My goal is just to retrieve data from Firebase and then output it as ListView in Android (No need to push anything back to the database). I tried to learn from the AndroidChat example and created my own class and my own Custom List Adapter class instead of Chat.java and ChatListAdapter.java (as in the orignal example). I also changed the references to my Firebase and resemble my data structure to that of https://android-chat.firebaseio-demo.com/. Below is my data structure: (Everything below is just the same as the AndroidChat example, just different variable names)
My own class:
package com.firebase.androidchat;
public class MenuItem {
private String food;
private String weekDay;
// Required default constructor for Firebase object mapping
@SuppressWarnings("unused")
private MenuItem() { }
MenuItem(String food, String weekDay) {
this.food = food;
this.weekDay = weekDay;
}
public String getFood() {
return food;
}
public String getWeekDay() {
return weekDay;
}
}
My own custom list adapter class:
package com.firebase.androidchat;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import com.firebase.client.Query;
public class MenuListAdapter extends FirebaseListAdapter<MenuItem> {
public MenuListAdapter(Query ref, Activity activity, int layout) {
super(ref, MenuItem.class, layout, activity);
}
@Override
protected void populateView(View view, MenuItem item) {
// Map a MenuItem object to an entry in our listview
String weekDay = item.getWeekDay();
TextView authorText = (TextView)view.findViewById(R.id.author);
authorText.setText(weekDay + ": ");
((TextView)view.findViewById(R.id.message)).setText(item.getFood());
}
}
My MainActivity in brief:
public class MainActivity extends ListActivity {
// TODO: change this to your own Firebase URL
private static final String FIREBASE_URL = "https://barlaurea.firebaseio.com/";
private Firebase ref;
private ValueEventListener connectedListener;
private MenuListAdapter menuListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup our Firebase ref
ref = new Firebase(FIREBASE_URL).child("menu");
}
@Override
public void onStart() {
super.onStart();
// Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
final ListView listView = getListView();
// Tell our list adapter that we only want 50 messages at a time
menuListAdapter = new MenuListAdapter(ref.limit(50), this, R.layout.chat_message);
listView.setAdapter(menuListAdapter);
menuListAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(menuListAdapter.getCount() - 1);
}
});
When I started the Run button, my device displayed a toast message "Connected to Firebase" but the screen remained blank and then crashed after a few seconds, showing a toast "Unfortunately, Firebase Chat has stopped". What did I miss here? Thank you!
Edit: Here is my LogCat view: Everything turn red at the "FAIL EXCEPTION: main line" Also the original AndroidChat run fine on my device..
02-07 16:17:35.105: D/dalvikvm(31766): GC_CONCURRENT freed 313K, 16% free 7795K/9223K, paused 14ms+16ms, total 65ms
02-07 16:17:36.000: D/dalvikvm(31766): GC_CONCURRENT freed 494K, 17% free 7874K/9479K, paused 2ms+3ms, total 34ms
02-07 16:17:36.365: D/dalvikvm(31766): GC_CONCURRENT freed 482K, 17% free 7923K/9543K, paused 19ms+21ms, total 142ms
02-07 16:17:36.495: D/AndroidRuntime(31766): Shutting down VM
02-07 16:17:36.495: W/dalvikvm(31766): threadid=1: thread exiting with uncaught exception (group=0x41fb02a0)
02-07 16:17:36.520: E/AndroidRuntime(31766): FATAL EXCEPTION: main
02-07 16:17:36.520: E/AndroidRuntime(31766): com.firebase.client.FirebaseException: Failed to bounce to type
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.androidchat.FirebaseListAdapter$1.onChildAdded(FirebaseListAdapter.java:63)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.client.core.ChildListenerContainer$1.run(ChildListenerContainer.java:52)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.os.Handler.handleCallback(Handler.java:615)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.os.Looper.loop(Looper.java:137)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.app.ActivityThread.main(ActivityThread.java:4947)
02-07 16:17:36.520: E/AndroidRuntime(31766): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 16:17:36.520: E/AndroidRuntime(31766): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-07 16:17:36.520: E/AndroidRuntime(31766): at dalvik.system.NativeStart.main(Native Method)
02-07 16:17:36.520: E/AndroidRuntime(31766): Caused by: com.shaded.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "weekday" (class com.firebase.androidchat.MenuItem), not marked as ignorable (2 known properties: , "weekDay", "food"])
02-07 16:17:36.520: E/AndroidRuntime(31766): at [Source: java.io.StringReader@42826fc8; line: 1, column: 13] (through reference chain: com.firebase.androidchat.MenuItem["weekday"])
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
02-07 16:17:36.520: E/AndroidRuntime(31766): ... 11 more
Upvotes: 4
Views: 4884
Reputation: 2211
The answer has been found here:
Firebase's AndroidChat example: Failed to bounce to type
In this case I should have typed weekDay not weekday in the firebase instance.
Upvotes: 5