IambicSam
IambicSam

Reputation: 11

firebase get data as array

I am trying to implement firebase into my Android app and I want to be able to pull all the entries in firebase in the order they display in into one string array to be put into a ListView Here is the raw JSON:

[ 5, "quot", "waaaaa", "also a quote", "oh this one is a little longer man", "gosh really long.   wow.  im very inspired.  golly gee wiz" ]

and the code I am using to try and get it:

public class MyActivity extends ListActivity {
ArrayList<String> LIST = new ArrayList<String>();
Boolean wow = true;
Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(context);
    updateList();


}
public void makeList(ArrayList<String> input){
    setListAdapter(new ArrayAdapter<String>(this, R.layout.mylist,input));
    ListView listView = getListView();
    listView.setTextFilterEnabled(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
public void updateList() {

    Firebase myFirebaseRef = new Firebase("https://admin1.firebaseio.com/");
    myFirebaseRef.child("0").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            System.out.println(snapshot.getValue());
            int length = Integer.parseInt(snapshot.getValue().toString());
            Firebase myFirebaseRef = new Firebase("https://admin1.firebaseio.com/");
            for(int i=1; i<length; i++) {
                String doIt = Integer.toString(i);
                myFirebaseRef.child(doIt).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        System.out.println(snapshot.getValue());
                        LIST.add(snapshot.getValue().toString());
                    }

                    @Override
                    public void onCancelled(FirebaseError error) {
                    }
                });
            }makeList(LIST); 
        }

        @Override
        public void onCancelled(FirebaseError error) {
        }
    });

    }

}

I was thinking that I could set the first (0th) object to be the number of entries and then cycle through the entire file using .getValue but when this is run I get out of memory exceptions and the app force closes. All I am sure of is that the relevant firebase stuff is the issue and not the ListView. Thanks for any tips.

Upvotes: 0

Views: 9142

Answers (1)

Kato
Kato

Reputation: 40582

Firstly, your data is stored in a JSON data object (i.e. not an array). You do not want to store sequential, numeric ids in distributed data.

To listen for the first n objects, utilize the query methods and limitToFirst.

int n = 10;
String URL = "https://<your instance>.firebaseio.com";
Firebase ref = new Firebase(URL);
Query queryRef = ref.orderByKey().limitToFirst(n);
queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
        Map<String, String> value = (Map<String, String)snapshot.getValue();
        System.out.println(snapshot.getKey() + " was " + value.get("message"));
    }
    // ....
});

Upvotes: 3

Related Questions