0

I am looping through data in Android, using Parse data. I came up with this as a way to get user information; the larger goal is to create a model of data that I can use in an array adapter, so I can create a custom list view (as described here) In the example, the data are hard-coded, not pulled from a database.

public static ArrayList<Midwifefirm> getUsers() {

    //Parse data to get users
    ParseQuery<ParseUser> query = ParseUser.getQuery();

    query.orderByAscending(ParseConstants.KEY_PRACTICE_NAME);

    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> users, ParseException e) {

            if (e == null) {

                final List<ParseUser> mMidwives;
                mMidwives = users;

                String usertype;

                String[] midwives = new String[mMidwives.size()];
                String[] yearsofexperience = new String[mMidwives.size()];
                String[] education = new String[mMidwives.size()];
                String[] philosophy = new String[mMidwives.size()];

                int i = 0;
                for (ParseUser user : mMidwives) {

                    usertype = user.getString("userType");

                    if (!Arrays.asList(midwives).contains(usertype) && usertype != "patient") {

                        midwives[i] = user.getString("practicename");
                        yearsofexperience[i] = user.getString("yearsofexperience");
                        education[i] = user.getString("education");
                        philosophy[i] = user.getString("practicephilosophy");

                        ArrayList<Midwifefirm> midwifefirm = new ArrayList<Midwifefirm>();
                        midwifefirm.add(new Midwifefirm(midwives[i], yearsofexperience[i], education[i], philosophy[i]));

                        return midwifefirm;
                    }
                }
            }
        }
    });
}

The intention is that for every user that does not have the type patient, collect this data about them, then store it in the arrayList.

On the return statement, though, there is an error: cannot return a value from a method with a void return type.

I may be over complicating this...read through various sources to get a model for this...in the end, I want to display a list of information about specific users, after the user makes a selection of a city...it would therefore display all the information about the medical practices in that city.

Thanks for your help

Michael

5
  • You are not closing your braces and you are returning twice. Is this a copy paste error or is it in the code? Commented Apr 24, 2015 at 20:08
  • Copy and paste error, I will update the code Commented Apr 24, 2015 at 20:21
  • why don't you declare arrays in class and then use setters and getters? Commented Apr 24, 2015 at 21:21
  • @shaz so: my mainActivity has: ArrayList<Midwifefirm> arrayOfUsers = Midwifefirm.getUsers(); call class where I am defining data, function getUsers: public static ArrayList<Midwifefirm> getUsers() {; within that function, I should declare ArrayList for each data point I want (education, experience, etc)? Commented Apr 24, 2015 at 22:23
  • yeah just declare the array in the class instead of in the method and keep everything same in the getUser function. also don't return anything from getUser method as array is in the class u can access from within the class Commented Apr 26, 2015 at 16:25

1 Answer 1

1

The code won't even compile in the form you sent it.

You make an asynchronous call when you use findInBackground. That means the code that calls it will just carry on after it calls it. It won't wait for it to finish, it doesn't care about it anymore.

The right way to deal with this type of code is by writing the logic you want and manipulating the results in the callback. So the first thing we change is the return type of getUsers to void.

Now getUsers merely says: "OK, please find this in background and from here I'll let you do your thing in done()".

public static void getUsers() {

    //Parse data to get users
    ParseQuery<ParseUser> query = ParseUser.getQuery();

    query.orderByAscending(ParseConstants.KEY_PRACTICE_NAME);

    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> users, ParseException e) {

            if (e == null) {
                // here you manipulate your list. you set up your data structures 
                // or whatever and call other methods from your `Activity` or whatever, 
                // passing the data as parameter
            }
        }
    });
}

You will need to update the code that calls getUsers and needs the list of users, because, as you see, it can't get the results straight away. You may be able to move some of that code in done.

LE: You may also want to remove the static modifier from your method.

Sign up to request clarification or add additional context in comments.

1 Comment

I realized I set this up wrong; I found this resource to help guide me androidbegin.com/tutorial/… which will help in addition to your answer

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.