Java 从Firebase数据库填充listview
我正试图从Firebase数据库在我的Android应用程序上填充Java 从Firebase数据库填充listview,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正试图从Firebase数据库在我的Android应用程序上填充列表视图。我正在使用(以及图像的后续存储)。我将记录放入数据库没有问题,但是检索信息并填充我的ListView被证明是很棘手的。这是我的密码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_adminfeed);
列表视图。我正在使用(以及图像的后续存储)。我将记录放入数据库没有问题,但是检索信息并填充我的ListView被证明是很棘手的。这是我的密码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adminfeed);
DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://bookstore-30213.firebaseio.com/All_Books");
final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, R.layout.listview_layout);
list = (ListView) findViewById(R.id.listView);
adapter = new CustomListAdapter(AdminFeed.this, bookModelList);
list.setAdapter(adapter);
}
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("All_Books");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject book = jsonArray.getJSONObject(i); //something goes wrong here, i think its possibly with the size of the reading
Book newBook = new Book();
newBook.setTitle(book.getJSONObject("title").getString("title"));
newBook.setImageURL(book.getString("imageURL"));
newBook.setAuthor(book.getJSONObject("author").getString("author"));
// adding event to events array
bookModelList.add(newBook);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u adminfeed);
DatabaseReference databaseRef=FirebaseDatabase.getInstance().getReferenceFromUrl(“https://bookstore-30213.firebaseio.com/All_Books");
最终ArrayAdapter myArrayAdapter=新的ArrayAdapter(这个,R.layout.listview_布局);
list=(ListView)findViewById(R.id.ListView);
adapter=新的CustomListAdapter(AdminFeed.this,bookModelList);
list.setAdapter(适配器);
}
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“所有书籍”);
for(int i=0;i
它返回到我的“bookModelList”是空的,但我不知道为什么,因为我显然在向其中添加一本书。有没有关于我哪里出错的提示?要解决这个问题,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference allBooksRef = rootRef.child("All_Books");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Book> bookModelList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Book book = ds.getValue(Book.class);
bookModelList.add(book);
}
ListView list = (ListView) findViewById(R.id.listView);
CustomListAdapter adapter = new CustomListAdapter(AdminFeed.this, bookModelList);
list.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
allBooksRef.addListenerForSingleValueEvent(valueEventListener);
DatabaseReference rootRef=FirebaseDatabase.getInstance().getReference();
DatabaseReference allBooksRef=rootRef.child(“所有书籍”);
ValueEventListener ValueEventListener=新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
List bookModelList=新建ArrayList();
对于(DataSnapshot ds:DataSnapshot.getChildren()){
Book Book=ds.getValue(Book.class);
bookModelList.add(book);
}
ListView列表=(ListView)findViewById(R.id.ListView);
CustomListAdapter=新的CustomListAdapter(AdminFeed.this,bookModelList);
list.setAdapter(适配器);
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){}
};
allBooksRef.addListenerForSingleValueEvent(valueEventListener);
请注意,从Firebase数据库获取数据不需要使用JSONObject
。您在响应对象中获得的内容可以添加到您的问题中吗感谢您的响应,但我收到了此错误。“java.lang.NullPointerException:尝试在com.example.apple.BookStore.AccountActivity.AdminApplication.AdminFeed$1.onDataChange(AdminFeed.java:93)上的空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'”请看我的最新答案。我已经在方法中声明了列表。它现在能用了吗?谢谢你的帮助,现在看起来很好!!虽然布局不好,但这是另一个问题,非常感谢!不客气,很高兴听到它起作用了!如果你认为我的回答对你有帮助,请考虑接受并投票表决。谢谢