[英]Retrieve data from Firebase into Listview
I am trying to retrieve data from Firebase but nothing pulls through. 我正在尝试从Firebase检索数据,但是没有结果。 The app is written successfully however when it tries to get the info back it just shows a blank screen. 该应用程序已成功编写,但是当尝试获取信息时,它仅显示空白屏幕。 My Firebase structure is Saica>Calendar>key>Data
我的Firebase结构是Saica>Calendar>key>Data
I have included a picture of my Firebase Structure 我已附上我的Firebase结构的图片
This is my code: 这是我的代码:
public class Admin_ViewEvent extends AppCompatActivity {
private ListView mListView;
private ArrayList<String> mMeetings = new ArrayList<>();
private Firebase mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin__view_event);
mRef = new Firebase("https://saica-sgb-77a4f.firebaseio.com/Meetings");
mListView = (ListView) findViewById(R.id.ListView);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMeetings);
mListView.setAdapter(arrayAdapter);
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
mMeetings.add(value);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
As per your old Screenshot Reference you can get date and description from parent Calendar node. 按照旧的屏幕快照参考,您可以从父日历节点获取日期和描述 。 You can use below snippet to get details from Meetings node. 您可以使用下面的代码片段从Meetings节点获取详细信息。
FirebaseDatabase.getInstance().getReference().child("Calendar").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
String date = (String) childSnapShot.child("date").getValue();
String desc = (String) childSnapShot.child("description").getValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Based on the line 基于行
mRef = new Firebase("https://saica-sgb-77a4f.firebaseio.com/Meetings");
it looks like you are using the wrong firebase sdk. 看来您使用的是错误的Firebase SDK。 It's either an old version or the java server sdk, but definitely not a recent android sdk. 它要么是旧版本,要么是Java服务器sdk,但绝对不是最新的android sdk。
Check you build.gradle
: you should have a line like this : 检查您的build.gradle
:您应该有这样一行:
compile "com.google.firebase:firebase-database:10.0.0"
To connect to your database and get a reference : 要连接到数据库并获得参考:
google-services.json
from the firebase console to your app/
folder. 从firebase控制台将文件google-services.json
下载到您的app/
文件夹中。 Create the database reference like this : 创建数据库引用,如下所示:
FirebaseDatabase db = FirebaseDatabase.getInstance(); DatabaseReference mRef = db.getReference("Meetings") ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.