How to extract a list of objects from Firebase DataSnapshot on android

How to extract a list of objects from Firebase DataSnapshot on android

Stefana Dominik Author: Stefana Dominik Date: 2022-08-17
How to extract a list of objects from Firebase DataSnapshot on android

All you need to know about How to extract a list of objects from Firebase DataSnapshot on android , in addintion to android - How to retrieve data from firebase database which is having nested array and objects structure? , android - How to extract the text from the selected item on the listView , android - How to extract all the user's username who are online from firebase database. , android - How to get a String List from Firebase to fill a Spinner

  1. How to extract a list of objects from Firebase DataSnapshot on android
  2. Question:

    I want to convert all Firebase DataSnapshot children to a list in android.

    Something like this:

    mFirebaseRef = new Firebase(FIREBASE_URL);
    
    mFirebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<String> td = (ArrayList<String>) dataSnapshot.getValue();
            //notifyDataSetChanged();
        }
    
        @Override
        public void onCancelled(FirebaseError firebaseError) {
    
        }
    });
    


    Solution 1:

    I hope below code works

    Firebase ref = new Firebase(FIREBASE_URL);
    
      ref.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot snapshot) {
              Log.e("Count " ,""+snapshot.getChildrenCount());
              for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                <YourClass> post = postSnapshot.getValue(<YourClass>.class);
                Log.e("Get Data", post.<YourMethod>());
              }
          }
          @Override
          public void onCancelled(FirebaseError firebaseError) {
              Log.e("The read failed: " ,firebaseError.getMessage());
          }
      });
    

    Solution 2:

    Firebase stores a sequence of values in this format:

    "-K-Y_Rhyxy9kfzIWw7Jq": "Value 1"
    "-K-Y_RqDV_zbNLPJYnOA": "Value 2"
    "-K-Y_SBoKvx6gAabUPDK": "Value 3"
    

    If that is how you have them, you are getting the wrong type. The above structure is represented as a Map, not as a List:

    mFirebaseRef = new Firebase(FIREBASE_URL);
    
    mFirebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();
    
            List<Object> values = td.values();
    
            //notifyDataSetChanged();
        }
    
        @Override
        public void onCancelled(FirebaseError firebaseError) {
    
        }
    });
    

    Solution 3:

    FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
     DatabaseReference databaseReference =    mFirebaseDatabase.getReference(FIREBASE_URL);
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
    
                    for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                        Log.v(TAG,""+ childDataSnapshot.getKey()); //displays the key for the node
                        Log.v(TAG,""+ childDataSnapshot.child(--ENTER THE KEY NAME eg. firstname or email etc.--).getValue());   //gives the value for given keyname
                    }
                }
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    

    Hope it helps!

  3. android - How to retrieve data from firebase database which is having nested array and objects structure?
  4. Question:

    I tried data extraction code from many places but everywhere i get how to retrieve data in a simple list. As the structure of my JSON is complex and i can't change it now, so please help me in retrieving data from firebase database. Please help as i am not able to understand Firebase code. Following is my code and JSON database: enter image description here

    CategoryModel.xml

    package com.example.firedb;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    import java.util.ArrayList;
    
    /**
     * Created by Android on 5/31/2017.
     */
    
    public class CategoryModel {
    
        private ArrayList<CategoryList> categoryList;
    
        public CategoryModel() {
        }
    
        public CategoryModel(ArrayList<CategoryList> categoryList) {
            this.categoryList = categoryList;
        }
    
        public ArrayList<CategoryList> getCategoryList() {
            return categoryList;
        }
    
        public void setCategoryList(ArrayList<CategoryList> categoryList) {
            this.categoryList = categoryList;
        }
    
    
        // CategoryList
    
        public  static class CategoryList {
    
            public int Category_id;
            public String Category_name;
            public ArrayList<String>Emails;
            public ArrayList<String>Epabx;
            public ArrayList<String>Category_Fax;
            public ArrayList<Persons> persons;
    
            public CategoryList() {
            }
            //constructor of CategoryList
    
            public CategoryList(int category_id, String category_name,
                                ArrayList<String> emails, ArrayList<String> epabx, ArrayList<String> category_Fax,
                                ArrayList<Persons> persons) {
                Category_id = category_id;
                Category_name = category_name;
                Emails = emails;
                Epabx = epabx;
                Category_Fax = category_Fax;
                this.persons = persons;
            }
    
            // getters and setters of CategoryList
    
            public int getCategory_id() {
                return Category_id;
            }
    
            public void setCategory_id(int category_id) {
                Category_id = category_id;
            }
    
            public String getCategory_name() {
                return Category_name;
            }
    
            public void setCategory_name(String category_name) {
                Category_name = category_name;
            }
    
            public ArrayList<String> getEmails() {
                return Emails;
            }
    
            public void setEmails(ArrayList<String> emails) {
                Emails = emails;
            }
    
            public ArrayList<String> getEpabx() {
                return Epabx;
            }
    
            public void setEpabx(ArrayList<String> epabx) {
                Epabx = epabx;
            }
    
            public ArrayList<String> getCategory_Fax() {
                return Category_Fax;
            }
    
            public void setCategory_Fax(ArrayList<String> category_Fax) {
                Category_Fax = category_Fax;
            }
    
            public ArrayList<Persons> getPersons() {
                return persons;
            }
    
            public void setPersons(ArrayList<Persons> persons) {
                this.persons = persons;
            }
    
    
        }
    
        //Persons
    
        public static class Persons {
    
            private int Person_ID;
            private String Name;
            private String Designation;
            private ArrayList<String> Office_Phone;
            private ArrayList<String> Residence_Phone;
            private String VOIP;
            private String Address;
            private ArrayList<String>Fax;
            private String Ext;
            private ArrayList<String>Extra_info;
            private String Category_name;
    
            public Persons() {
            }
    
            // Constructor of Persons
            public Persons(int person_ID, String name, String designation, ArrayList<String> office_Phone,
                           ArrayList<String> residence_Phone, String VOIP, String address, ArrayList<String> fax, String ext,
                           ArrayList<String>extra_info, String category_name) {
                Person_ID = person_ID;
                Name = name;
                Designation = designation;
                Office_Phone = office_Phone;
                Residence_Phone = residence_Phone;
                this.VOIP = VOIP;
                Address = address;
                Fax = fax;
                Ext = ext;
                Extra_info=extra_info;
                Category_name=category_name;
            }
    
            // Getter and Setters of Persons
    
            public int getPerson_ID() {
                return Person_ID;
            }
    
            public void setPerson_ID(int person_ID) {
                Person_ID = person_ID;
            }
    
            public String getName() {
                return Name;
            }
    
            public void setName(String name) {
                Name = name;
            }
    
            public String getDesignation() {
                return Designation;
            }
    
            public void setDesignation(String designation) {
                Designation = designation;
            }
    
            public ArrayList<String> getOffice_Phone() {
                return Office_Phone;
            }
    
            public void setOffice_Phone(ArrayList<String> office_Phone) {
                Office_Phone = office_Phone;
            }
    
            public ArrayList<String> getResidence_Phone() {
                return Residence_Phone;
            }
    
            public void setResidence_Phone(ArrayList<String> residence_Phone) {
                Residence_Phone = residence_Phone;
            }
    
            public String getVOIP() {
                return VOIP;
            }
    
            public void setVOIP(String VOIP) {
                this.VOIP = VOIP;
            }
    
            public String getAddress() {
                return Address;
            }
    
            public void setAddress(String address) {
                Address = address;
            }
    
            public ArrayList<String> getFax() {
                return Fax;
            }
    
            public void setFax(ArrayList<String> fax) {
                Fax = fax;
            }
    
            public String getExt() {
                return Ext;
            }
    
            public void setExt(String ext) {
                Ext = ext;
            }
    
            public ArrayList<String> getExtra_info() {
                return Extra_info;
            }
    
            public void setExtra_info(ArrayList<String> extra_info) {
                Extra_info = extra_info;
            }
    
            public String getCategory_name() {
                return Category_name;
            }
    
            public void setCategory_name(String category_name) {
                Category_name = category_name;
            }
    
        }
    
    }
    

    CardviewActivity: Earlier i used an asset file but now i want to get data from firebase whose code is ambiguous online

    package com.example.firedb;
    
    import android.content.Intent;
    import android.graphics.Color;
    import android.graphics.drawable.Drawable;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.text.Editable;
    import android.text.LoginFilter;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    
    import static android.R.attr.button;
    
    
    public class CardViewActivity extends AppCompatActivity {
        Toolbar mActionBarToolbar;
        private RecyclerView mainRecyclerView;
        private RecyclerView.Adapter mainAdapter;
        private RecyclerView.LayoutManager mainLayoutManager;
        private static String LOG_TAG = "CardViewActivity";
        EditText inputSearchMain;
        private ArrayList<CategoryModel.CategoryList> categoryLists;
        TextView toolbar_title_main;
        ImageView back_cardviewActivity;
    //    DatabaseHandler db;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_card_view);
    
    //         db = new DatabaseHandler (CardViewActivity.this);
    
            mActionBarToolbar = (Toolbar) findViewById(R.id.tool_bar);
            toolbar_title_main=(TextView)findViewById(R.id.toolbar_title);
    
    
    //        mActionBarToolbar.setTitle("Hry. Govt. Telephone Directory");
           // mActionBarToolbar.setLogo(R.drawable.logotoolbar);
    
    //        mActionBarToolbar.setTitleMargin(5,2,2,2);
            setSupportActionBar(mActionBarToolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            toolbar_title_main.setText("Hry. Govt. Telephone Directory");
            back_cardviewActivity=(ImageView)findViewById(R.id.back);
            back_cardviewActivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
    
            categoryLists=new ArrayList<CategoryModel.CategoryList>();
            categoryLists.addAll(getmcategoryset());
            mainRecyclerView=(RecyclerView)findViewById(R.id.recyclerView_Main);
            mainRecyclerView.setHasFixedSize(true);
            mainLayoutManager=new LinearLayoutManager(this);
            mainRecyclerView.setLayoutManager(mainLayoutManager);
    //        Log.d( "onCreate: ", "List Size: "+categoryLists.size());
            mainAdapter=new RecyclerViewAdapterMain(getmcategoryset());
            mainRecyclerView.setAdapter(mainAdapter);
            inputSearchMain = (EditText) findViewById(R.id.inputSearchMain);
    
            addTextListener();
        }
    
        public void addTextListener(){
    
            inputSearchMain.addTextChangedListener(new TextWatcher() {
    
                public void afterTextChanged(Editable s) {}
    
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
                public void onTextChanged(CharSequence query, int start, int before, int count) {
    
                    query = query.toString().toLowerCase();
    
                    final ArrayList<CategoryModel.CategoryList> filteredList = new ArrayList<CategoryModel.CategoryList>();
    
                    for (int i = 0; i < categoryLists.size(); i++) {
    
                        final String text = categoryLists.get(i).getCategory_name().toLowerCase();
                        if (text.contains(query)) {
    
                            filteredList.add(categoryLists.get(i));
                        }
                    }
    
                    mainRecyclerView.setLayoutManager(new LinearLayoutManager(CardViewActivity.this));
                    mainAdapter = new RecyclerViewAdapterMain(filteredList);
                    mainRecyclerView.setAdapter(mainAdapter);
                    mainAdapter.notifyDataSetChanged();  // data set changed
                }
            });
        }
    
        private ArrayList<CategoryModel.CategoryList> getmcategoryset() {
    //        JSONObject obj = new JSONObject(readJSONFromAsset());
    
            try {
    
                ArrayList<CategoryModel.CategoryList>categoryList = new ArrayList<CategoryModel.CategoryList>();
                JSONObject jsonObject = new JSONObject(readJSONFromAsset());
    
                JSONArray categoryArray = jsonObject.getJSONArray("Category");
                Log.d("getmcategoryset", "category count: "+categoryArray.length());
                for (int i = 0; i < categoryArray.length(); i++)
                {
                    JSONObject job = categoryArray.getJSONObject(i);
    
                    int categoryId = job.getInt("Category_id");
                    String categoryName = job.getString("Category_name");
    
                    //this is for email array
                    ArrayList<String> emails = new ArrayList<>();
                    JSONArray emailArray = job.getJSONArray("Emails");
                    for (int j = 0; j< emailArray.length(); j++){
    //                    JSONObject jobE = emailArray.getString(j);
                        emails.add(emailArray.getString(j));
                    }
    
                    //This i for Epabx array
                    ArrayList<String> epabx = new ArrayList<>();
                    JSONArray epabxArray = job.getJSONArray("Epabx");
                    for (int j = 0; j < epabxArray.length(); j++){
    //                    JSONObject jobE = epabxArray.getString(j);
                        epabx.add(epabxArray.getString(j));
                    }
    
                    //This i for Category_Fax array
                    ArrayList<String> category_Fax = new ArrayList<>();
                    JSONArray category_FaxJson = job.getJSONArray("Category_Fax");
                    for (int j = 0; j < category_FaxJson.length(); j++){
    //                    JSONObject jobE = category_FaxJson.getString(j);
                        category_Fax.add(category_FaxJson.getString(j));
                    }
    
                    //This i for Persons array
                    ArrayList<CategoryModel.Persons> personsList = new ArrayList<>();
                    JSONArray personsArray = job.getJSONArray("Persons");
                    for (int j = 0; j < personsArray.length(); j++){
                        JSONObject jobIn = personsArray.getJSONObject(j);
    
                        int Person_ID = jobIn.getInt("Person_ID");
                        String Name = jobIn.getString("Name");
                        String Designation = jobIn.getString("Designation");
    
                        //this is for Office_Phone array
                        ArrayList<String>Office_Phone = new ArrayList<>();
                        JSONArray office_Phone = jobIn.getJSONArray("Office_Phone");
                        for (int k=0; k < office_Phone.length(); k++)
                        {
                            Office_Phone.add(office_Phone.getString(k));
                        }
    
                        //this is for Residence_Phone array
                        ArrayList<String>Residence_Phone = new ArrayList<>();
                        JSONArray residence_Phone = jobIn.getJSONArray("Residence_Phone");
                        for (int k=0; k < residence_Phone.length(); k++)
                        {
                            Residence_Phone.add(residence_Phone.getString(k));
                        }
    
                        String VOIP = jobIn.getString("VOIP");
                        String Address = jobIn.getString("Address");
    
    
                        //this is for Fax array
                        ArrayList<String>Fax = new ArrayList<>();
                        JSONArray fax = jobIn.getJSONArray("Fax");
                        for (int k=0; k < fax.length(); k++)
                        {
                            Fax.add(fax.getString(k));
                        }
    
                        String Ext = jobIn.getString("Ext");
    
                        //this is for Extra_info array
                        ArrayList<String>Extra_info = new ArrayList<>();
                        JSONArray extra_info = jobIn.getJSONArray("Extra_info");
                        for (int k=0; k < extra_info.length(); k++)
                        {
                            Extra_info.add(extra_info.getString(k));
                        }
    
                        personsList.add(new CategoryModel.Persons(Person_ID, Name, Designation, Office_Phone, Residence_Phone,
                                VOIP, Address, Fax, Ext,Extra_info,categoryName));
    //                    db.addPerson(new CategoryModel.Persons(Person_ID, Name, Designation, Office_Phone, Residence_Phone,
    //                            VOIP, Address, Fax, Ext,Extra_info));
                    }
    
                    //here your Category[] value store in categoryArrayList
                    categoryList.add(new CategoryModel.CategoryList(categoryId, categoryName, emails, epabx, category_Fax, personsList));
    
    //                db.addCategory(new CategoryModel.CategoryList(categoryId, categoryName, emails, epabx, category_Fax, personsList));
    
                    Log.i("categoryList size = ", ""+categoryArray.length());
                    Log.i("cat_name=",""+categoryName);
                }
    
                if (categoryList != null)
                {
                    Log.i("categoryList size = ", ""+categoryArray.length());
                }
                return categoryList;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    //        ((RecyclerViewAdapterMain) mainAdapter).setOnItemClickListener(new RecyclerViewAdapterMain()
    //                .CategoryClickListener() {
    //            public void onItemClick(int position, View v) {
    //                Log.i(LOG_TAG, " Clicked on Item " + position);
    //            }
    //        });
        }
    
    
    }
    

    RecyclerViewAdapter:

    package com.example.firedb;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    
    public class RecyclerViewAdapterMain extends RecyclerView.Adapter<RecyclerViewAdapterMain.CategoryObjectHolder> {
        private static String LOG_TAG = "categoryRecyclrVwAdptr";
        private  ArrayList<CategoryModel.CategoryList> mcategoryset;
        private static CategoryClickListener categoryClickListener;
    
        public static class CategoryObjectHolder extends RecyclerView.ViewHolder  {
            TextView category_name;
            /*TextView category_emails;
            TextView category_epabx;
            TextView category_fax;*/
    
            public CategoryObjectHolder(View itemView){
                super(itemView);
                category_name=(TextView)itemView.findViewById(R.id.category_name);
                /*category_emails=(TextView)itemView.findViewById(R.id.category_emails);
                category_epabx=(TextView)itemView.findViewById(R.id.category_epabx);
                category_fax=(TextView)itemView.findViewById(R.id.category_fax);*/
                Log.i(LOG_TAG, "Adding Listener");
    
            }
    //        @Override
    //        public void onClick(View v) {
    //            categoryClickListener.onItemClick(getAdapterPosition(), v);
    //        }
        }
    //    public void setOnItemClickListener(CategoryClickListener categoryClickListener) {
    //        this.categoryClickListener = categoryClickListener;
    //    }
    
        public RecyclerViewAdapterMain(ArrayList<CategoryModel.CategoryList> myDataset) {
            mcategoryset = myDataset;
        }
    
        public CategoryObjectHolder onCreateViewHolder(ViewGroup parent,int viewType){
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row_main_activity,parent,false);
            CategoryObjectHolder categoryObjectHolder=new CategoryObjectHolder(view);
            return categoryObjectHolder;
        }
    
        @Override
        public void onBindViewHolder(CategoryObjectHolder holder, final int position) {
            /*final StringBuilder stringBuilder_emails = new StringBuilder();
            for (String email : mcategoryset.get(position).getEmails()) {
                if (!stringBuilder_emails.toString().isEmpty()) {
                    stringBuilder_emails.append(", ");
                }
                stringBuilder_emails.append(email);
            }
    
            final StringBuilder stringBuilder_Epabx = new StringBuilder();
            for (String epabx : mcategoryset.get(position).getEpabx()) {
                if (!stringBuilder_Epabx.toString().isEmpty()) {
                    stringBuilder_Epabx.append(", ");
                }
                stringBuilder_Epabx.append(epabx);
            }
    
            final StringBuilder stringBuilder_Category_Fax = new StringBuilder();
            for (String category_Fax : mcategoryset.get(position).getCategory_Fax()) {
                if (!stringBuilder_Category_Fax.toString().isEmpty()) {
                    stringBuilder_Category_Fax.append(", ");
                }
                stringBuilder_Category_Fax.append(category_Fax);
            }*/
    
            holder.category_name.setText(mcategoryset.get(position).getCategory_name());
            /*holder.category_emails.setText(stringBuilder_emails.toString());
            holder.category_epabx.setText(stringBuilder_Epabx.toString());
            holder.category_fax.setText(stringBuilder_Category_Fax.toString());*/
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Intent i = new Intent (v.getContext(), PeopleListActivity.class);
                    i.putParcelableArrayListExtra("Persons",mcategoryset.get(position).getPersons());
                    i.putStringArrayListExtra("emails",mcategoryset.get(position).getEmails());
                    //i.putExtras(b);
                    v.getContext().startActivity(i);
    
                }
            });
        }
        public void addItem(CategoryModel.CategoryList dataObj, int index) {
            mcategoryset.add(index, dataObj);
            notifyItemInserted(index);
        }
    
        public void deleteItem(int index) {
            mcategoryset.remove(index);
            notifyItemRemoved(index);
        }
    
        @Override
        public int getItemCount() {
            return mcategoryset.size();
        }
    
        public interface CategoryClickListener {
            public void onItemClick(int position, View v);
        }
    }
    


    Solution 1:

    The only way to get data from Firebase Database it's using a ValueEventListener. Firebase work with asynchronous calls, so you can't call the function, get the data and use it. You have to set a ValueEventListener in a specific database reference.

    For your actual problem, you have to create an array, set the Firebase Reference, add the ValueEventListener and inside add each occurrence of CategoryList in the array. Finally , still inside of the ValueEventListener, you can create your CategoryModel, with the CategoryList's array as parameter, and update your UI, to see the data in CategoryModel:

    CategoryModel categoryModel;
    ArrayList<CategoryList> array = new ArrayList<>();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference categoryRef = database.getReference("Category");
    categoryRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                CategoryList categoryList = childSnapshot.getValue(CategoryList.class);
                array.add(categoryList);
            }
            categoryModel = new CategoryModel(array);
            // Method to show the data in category model, 
            // usually populating an ListView or something
            updateUI()
        }
    
        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    

    For have this working, your CategoryList class has to implement all the setters of his members so "datasnapshot.getValue()" works. Another approach can be, create a Constructor in CategoryList that recibe a DataSnapshot in this way:

    public CategoryList(DataSnapshot snapshot){
        Category_id = snapshot.child("Category_id").getValue(int.class);
        Category_name = snapshot.child("Category_name").getValue(String.class);
        GenericTypeIndicator<List<String>> stringList = new GenericTypeIndicator<List<String>>() {};
        Emails = snapshot.child("Emails").getValue(stringList);
        Epabx= snapshot.child("Epabx").getValue(stringList);
        Category_Fax= snapshot.child("Category_Fax").getValue(stringList);
        GenericTypeIndicator<List<Persons>> personsList = new GenericTypeIndicator<List<Persons>>() {};
        persons = snapshot.child("Persons").getValue(personsList);
    }
    

    If you create the constructor, you have to replace this line:

    CategoryList categoryList = childSnapshot.getValue(CategoryList.class);
    

    with this:

    CategoryList categoryList = new CategoryList(childSnapshot);
    

    You can see that I use GenericTypeIndicator to work with collections of data, this is a helper class provide for Firebase to work with Lists, Maps, Sets or another Collection.

    Solution 2:

    Just a recommendation! This is not a good data structure, you should denormalize your data.Then you can observe your data by ChildEventListener or ValueEventListener.

    For example:

    -Category
    --CategoryId
    ---CategoryList
    ---Persons
    ----user1: true
    ----user2: true, etc.
    
    -Users
    --user1Id
    ---user1details
    --user2Id
    ---user2details, etc.
    

    Here is some useful links about denormalizing your data

    Link1, Link2

  5. android - How to extract the text from the selected item on the listView
  6. Question:

    I have a listview with some items. I would like to get the text from the selected item.

    Here is my list adapter and the onItemClickListener:

    ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(new ArrayAdapter<Country>(
                this,R.layout.list_black_text,R.id.list_content, values));
    
    
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    ??????
        }});
            }
    

    Could you tell me please how to get the String from the selected item.

    the method ((TextView) view).getText() does not work, i have a

    ClassCastException: android.widget.LinearLayout
    

    I have found the solution, maybe somebody will need it:

    ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(new ArrayAdapter<Country>(
                this,R.layout.list_black_text,R.id.list_content, values));
    
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.list_content);
                String text = textView.getText().toString(); 
                System.out.println("Choosen Country = : " + text);
    
        }});
    


    Solution 1:

    Use this:

    String selectedFromList = (String) (lv.getItemAtPosition(position));
    

    Whatever the datatype you are having in your list, cast accordingly.

    Hope it will help. :)

    Solution 2:

    Just a recommendation! This is not a good data structure, you should denormalize your data.Then you can observe your data by ChildEventListener or ValueEventListener.

    For example:

    -Category
    --CategoryId
    ---CategoryList
    ---Persons
    ----user1: true
    ----user2: true, etc.
    
    -Users
    --user1Id
    ---user1details
    --user2Id
    ---user2details, etc.
    

    Here is some useful links about denormalizing your data

    Link1, Link2

    Solution 3:

    The other answers look good, but I thought I'd wrap everything up into one complete answer.

    There are multiple ways to achieve this and it also depends on whether you are getting text from simple listView or from Custom ListView(with custom_list_item.xml).

    For Simple ListView

    lv.setOnItemClickListener(new OnItemClickListener() {
    
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            String text = lv.get(position).tostring().trim();//first method 
            final String text = ((TextView)view).getText();// second method
    }});
    

    For Custom ListView

    lv.setOnItemClickListener(new OnItemClickListener() {
    
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            TextView textView = (TextView) view.findViewById(R.id.list_content);
    //where list_content is the id of TextView in listview_item.xml
    
    }});
    

    Problem with others Answers

    @Android Killer string Casting is missing.

    @Rishi doesn't give detail about using R.id.list_content

  7. android - How to extract all the user's username who are online from firebase database.
  8. Question:

    I want to extract all the user's username (highlighted blue) who are online (highlighted red) from a firebase database to a list view.

    enter image description here


    Solution 1:

    Just tested this, and it works!

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference reference = database.getReference("drivers");
    Query query = reference.orderByChild("online").equalTo("true");
    
    
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, dataSnapshot.toString());
        }
    
        public void onCancelled(DatabaseError databaseError) { }
    });
    

    Solution 2:

    I found the answer to the question, thanks to A. Omar and Frank van Puffelen for the help:

        DatabaseReference ref = FirebaseDatabase.getInstance()
                .getReferenceFromUrl("https://<your-app>.firebaseio.com/drivers/");
    
        ref.addValueEventListener(new ValueEventListener()
        {
    
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot driverSnapshot: dataSnapshot.getChildren())
                {
                    if(driverSnapshot.child("online").getValue().toString()=="true"){
                    Log.d(TAG, driverSnapshot.getKey() + " - " + driverSnapshot.child("online").getValue().toString());
                    }
                }
            }
    
            public void onCancelled(FirebaseError firebaseError) {
    
            }
        });
    

    this will print only the online users like:

    2332424 - true

  9. android - How to get a String List from Firebase to fill a Spinner
  10. Question:

    my database is arranged like this:

    enter image description here

    I want to get all the values from areaName as a String List or any form of list and use it to populate a Spinner. The problem I am getting is that it only retrieves the last value (in this case "New York"). This is my actual code:

    fDatabaseRoot.child("areas").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
                    String areaName = areaSnapshot.child("areaName").getValue(String.class);
    
                    Spinner areaSpinner = (Spinner) findViewById(R.id.spinner);
                    final String[] areas = {areaName};
                    ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(UAdminActivity.this, android.R.layout.simple_spinner_item, areas);
                    areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    areaSpinner.setAdapter(areasAdapter);
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    


    Solution 1:

    I didn't test that code but, more or less, that is what you have to do:

    fDatabaseRoot.child("areas").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Is better to use a List, because you don't know the size 
            // of the iterator returned by dataSnapshot.getChildren() to
            // initialize the array
            final List<String> areas = new ArrayList<String>();
    
            for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
                String areaName = areaSnapshot.child("areaName").getValue(String.class);
                areas.add(areaName);
            }
    
            Spinner areaSpinner = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(UAdminActivity.this, android.R.layout.simple_spinner_item, areas);
            areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            areaSpinner.setAdapter(areasAdapter);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
    
        }
    });
    

    I hope it's helps!

    Regards.