1

I'm stuck, I try to get an Array in Parse. I succeed to get it however I can't return it to use it in another method.

Do someone know what should I do ?

Retrieved ["international","entrepreneurship"]

public class CardsActivity extends AppCompatActivity {
ParseUser currentUser = ParseUser.getCurrentUser();
String test = currentUser.getObjectId();


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cards);

    // Specify which class to query
    ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
    query.selectKeys(Arrays.asList("tastes"));
    // Specify the object id

    query.getInBackground(test, new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {

                if (e == null) {

                    ArrayList<String> userTastesGot = (ArrayList<String>) object.get("tastes");

                    Log.d("User", "Retrieved " + userTastesGot);

                } else {
                    Toast.makeText(CardsActivity.this, "Nous n'avons pas trouvés vos goûts", Toast.LENGTH_SHORT).show();
                }
            }

    });
2
  • What do you plan to do with the data? Do you plan to update a view? Commented Apr 10, 2016 at 14:04
  • I plan to use this listArray in another method to select randomly one of the elements inside (for instance 'entrepreneurship') this will not be displayed in a view Commented Apr 10, 2016 at 14:14

1 Answer 1

1

You can't return it from onCreate, no. I wouldn't even retrieve it in onCreate unless you can be certain that it won't be needed until it has been retrieved. I would do something like this:

interface Callback<T> {

    void success(T result);
    void failure(Exception error);
}

void getUserTastes(Callback<ArrayList<String>> callback){

// Note the special way to get a query for the user table
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.selectKeys(Arrays.asList("tastes"));
// TODO: Specify the object id

query.getInBackground(test, new GetCallback<ParseUser>() {
    public void done(ParseUser object, ParseException e) {

            if (e == null) {

                ArrayList<String> userTastesGot = (ArrayList<String>) object.get("tastes");

                Log.d("User", "Retrieved " + userTastesGot);

                callback.success(userTastesGot);

            } else {
                callback.failure(e);
            }
        }

});

}

Use whatever protection levels are appropriate.

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

2 Comments

Thanks however how I call this method ? Do I call it in onCreate method ?
Call it from wherever you need to have the results.

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.