Rhynoboy2009
Rhynoboy2009

Reputation: 140

Parse.com pull array data

I'm making a friends list in Parse using an Array. I'm having trouble parsing the array within the users table. Array Column

How can I get each object in the array and display it's value?

Upvotes: 2

Views: 2745

Answers (2)

cYrixmorten
cYrixmorten

Reputation: 7108

Since the array is Pointers, they do not contain any data, thus you need to fetch the objects by doing something like this:

    List<ParseObject> objects = user.getList("friendsArray");
    if (objects == null)
        return;

    ParseObject.fetchAllIfNeededInBackground(objects, new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e != null) {
                Log.e(TAG, e.getMessage(), e);
                return;
            }

            // friends are each filled with data 

        }
    });

As userAndroid correctly mention, if there is no values in 'friendsArray' it might return null:

Returns null if there is no such key or if the value can't be converted to a List

This could be solved by checking if the key exists user.has("friendsArray"), I have updated the code to simply return if the objects list is null.

Upvotes: 0

userAndroid
userAndroid

Reputation: 586

The above answer will terminate the app when the array will have null value.You have to check that the value of that perticular column is null or not .See below code

List<String> list11 =  new ArrayList<String>();
ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("TABLE NAME");
pQuery.findInBackground(new FindCallback<ParseObject>() {

    @Override
    public void done(List<ParseObject> list, ParseException e) {
        if (e==null) {
        if (list.size()>0) {
            ParseObject p = list.get(0);
            if (p.getList("friendsArray")!=null) {
                list11 =  p.getList("friendsArray");
            }
            else
            {
                list11= null;
            }
            }}
                    }
                    });

use getList method to get the data from array column of parse table

now if you want to get all individual data of parsed array ,you can simply apply looping on **list11**.

For more info see following link:

ParseObject

How to fetch value from parse class containing Array type column of strings in android

Upvotes: 3

Related Questions