无法从 Firebase 将数据显示为 ListView
Posted
技术标签:
【中文标题】无法从 Firebase 将数据显示为 ListView【英文标题】:Fail to display data as ListView from Firebase 【发布时间】:2014-03-04 19:16:54 【问题描述】:我的目标只是从 Firebase 检索数据,然后在 android 中将其作为 ListView 输出(无需将任何内容推回数据库)。我尝试从 AndroidChat 示例中学习,并创建了自己的类和自定义列表适配器类,而不是 Chat.java 和 ChatListAdapter.java(如在原始示例中)。我还更改了对 Firebase 的引用,并将我的数据结构类似于 https://android-chat.firebaseio-demo.com/ 的数据结构。下面是我的数据结构:(下面的一切都和AndroidChat的例子一样,只是变量名不同)
我自己的班级:
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;
我自己的自定义列表适配器类:
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());
我的 MainActivity 简介:
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);
);
当我启动“运行”按钮时,我的设备显示一条 toast 消息“已连接到 Firebase”,但屏幕保持空白,然后在几秒钟后崩溃,并显示一条 toast“不幸的是,Firebase 聊天已停止”。我在这里错过了什么?谢谢!
编辑:这是我的 LogCat 视图:在“FAIL EXCEPTION: main line”处一切都变红了 原来的 AndroidChat 在我的设备上也可以正常运行..
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
【问题讨论】:
当崩溃发生时你能发布你的 logcat 输出吗? 嗨@KhawarRaza,感谢您指出,我已将其附加到问题中。 【参考方案1】:答案已经在这里找到了:
Firebase's AndroidChat example: Failed to bounce to type
在这种情况下,我应该在 firebase 实例中输入 weekDay 而不是 weekday。
【讨论】:
以上是关于无法从 Firebase 将数据显示为 ListView的主要内容,如果未能解决你的问题,请参考以下文章
从 firebase 读取数据,并在 listview 中填充
如果无法从 Firebase 实时数据库中检索数据,则尝试在 UITableView 上显示消息