0

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.

2
  • I'd suggest starting with reading over firebase.com/docs/android/guide/understanding-data.html and firebase.com/blog/… Commented Mar 9, 2015 at 1:29
  • 1
    Yep. Definitely dig into the docs and save yourself some pain and thrashing here. Most everything about this looks like it could be improved with a bit of docs love. Commented Mar 9, 2015 at 19:40

1 Answer 1

3

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"));
    }
    // ....
});
Sign up to request clarification or add additional context in comments.

5 Comments

This makes sense but I'm getting an error "is not abstract and does not override abstract method onCancelled(com.firebase.client.FirebaseError) in com.firebase.client.ChildEventListener"
@IambicSam the // ... in that example expects you to fill in the rest. The methods to override are covered in the getting started topic.
I've been able to make this work. The only issue I'm having is I'm trying to specifically grab the push() key for the individual item clicked in a listview. I looked at my debug info, and I don't see the keys stored in the list.
Ugh. This is bad design, imo: "In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase clients will render it as an ArrayList." What this implies: If the developer doesn't explicitly handle this case, the type of the object could unexpectedly change (from object to array) when a user just adds another entry at some point. A database object should not change (client-received) types just because it gains another child!
So don't use sequential numeric ids. Also, this isn't the right forum to discuss design decisions. Also, I agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.