如何将Firebase数据放入ListView? [英] How to get Firebase Data into a ListView?
问题描述
我正在做一个Android应用程序.我已成功写入Firebase,但是从Firebase读取到ListView时遇到了问题.如果任何人都可以协助将数据读回ListView中的应用程序,如果我只有一项可以尝试读回,则在我尝试获取所有三项的情况下,该应用程序将失败. 我附带了一张数据库图片,以了解结构和代码.
I am doing an Android App. I have been successful in writing to Firebase, I however am having a problem reading from Firebase into a ListView. If anyone can assist in getting the data to read back into the app into the ListView, If i just have one item to try and read back it works if I try to get all three items the app fails. I have included a picture of the Database to get an idea of the structure and my code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
public class Admin_ViewEvent extends AppCompatActivity {
// private TextView textViewPersons;
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);
//textViewPersons = (TextView) findViewById(R.id.textViewPersons);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMeetings);
mListView.setAdapter(arrayAdapter);
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for(DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
String date = (String) childSnapShot.child("address").getValue();
String datetime = (String) childSnapShot.child("datetime").getValue();
String name = (String) childSnapShot.child("name").getValue();
mMeetings.add(date);
mMeetings.add(datetime);
mMeetings.add(name);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
}
}
推荐答案
使用ChildEventListener并输入以下代码:
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String(this,android.R.layout.simple_list_item_1, mMeetings);
mListView.setAdapter(arrayAdapter);
Use ChildEventListener and put your this code:
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String(this,android.R.layout.simple_list_item_1, mMeetings);
mListView.setAdapter(arrayAdapter);
在onChildAdded内部....
还创建Model类以从Firebase获取数据
公共类用户{公共字符串名称;公共字符串日期;公共字符串时间;//调用
// DataSnapshot.getValue(User.class)
public User() {}public User(String name, String time, String date) {this.name = name;
this.time= time;this.date=date;}
所需的默认构造函数
谢谢,它将为您提供帮助.
inside the onChildAdded....
Also creat Model class to get data from firebase
public class User {public String name;public String date;public String time;// Default constructor required for calls to // DataSnapshot.getValue(User.class)
public User() {}public User(String name, String time, String date) {this.name = name;
this.time= time;this.date=date;}
thanks it will help you..
这篇关于如何将Firebase数据放入ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!