HTG
HTG

Reputation: 584

ListView not displaying any data from Parse

I've created a basic query that will pull a name, username, and an image down from the Parse cloud and load it into a ListView. There are 3 test users in the cloud at the moment. When I perform the query, the information is pulled and is able to be seen through the Logcat. But when attempting to load it into the listview using my Custom Base Adapter, nothing is displayed at all. It just returns an empty space where the listview should be.

I've isolated the problem down to this method that I believe is causing the problem:

public void findUserFriends() {

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereNotEqualTo("username", "~");
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(final List<ParseUser> objects, ParseException e) {
            if (e == null) {
                // The query was successful.

                ArrayList myList = new ArrayList();

                for (int i = 0; i < objects.size(); i++) {

                    FriendListData fld = new FriendListData();

                    fld.setUsername(objects.get(i).getUsername());
                    fld.setFullname(objects.get(i).getString("name"));

                    final ParseFile picture = objects.get(i).getParseFile("profilepicture");

                    if (picture == null) {
                        Toast.makeText(getApplicationContext(), "No File Found", Toast.LENGTH_LONG).show();
                        return;
                    }

                    picture.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] data, ParseException e) {
                            if (e == null) {
                                if (data.length == 0) {
                                    // data found, but nothing to extract. bad image or upload?
                                    Toast.makeText(getApplicationContext(), "Image is bad. Please Try Again Later", Toast.LENGTH_LONG).show();
                                    return;
                                }

                                // SUCCESS
                                // convert data and display in an imageview

                                FriendListData fld = new FriendListData();
                                Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

                                fld.setProfilePicture(bmp);

                            } else {
                                Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_LONG).show();
                            }
                        }
                    });

                    myList.add(fld);

                }

            } else {
                Toast.makeText(getApplicationContext(), "No Users Found.", Toast.LENGTH_LONG).show();
            }

        }
    });

}

And then I do this inside the onCreate method:

lvDetail = getListView();
findUserFriends();
lvDetail.setAdapter(new FriendBaseAdapter(context, myList));

Like said, all the needed data is pulled from the cloud and can be seen when I use my logcat. They just don't load into the ListView. I'm not sure what I'm doing wrong.

All Help Is Appreciated.

Upvotes: 0

Views: 122

Answers (1)

Oleg Osipenko
Oleg Osipenko

Reputation: 2430

Seems that you're setting adapter earlier than your asynchronous loading completes - try to move this line lvDetail.setAdapter(new FriendBaseAdapter(context, myList)); inside your callback after myList.add(fld);

Upvotes: 1

Related Questions