简体   繁体   中英

Populate a listview from Firebase Database

I'm trying to populate a ListView on my Android app from a Firebase Database. I'm using (and subsequent Storage for my images). I'm putting records into the Database no problem, but retrieving the info back and populating my ListView is proving to be tricky. Here's my code:

    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();
    }

It's returning that my 'bookModelList' is empty, but I've no idea why as I'm clearly adding a book to it. Any pointers as to where I'm going wrong?

To solve this, please use the following code:

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);

Please note, that there is no need to use a JSONObject in order to get data from a Firebase database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM