Evan Ngo
Evan Ngo

Reputation: 254

Retrieving data from Parse.com to ArrayList<Object>

I have to try download Data into an ArrayList . When i call it inside done() method it's ok, but when i call it outside done() it's will be null. How i can fix it?

        ParseQuery<ParseObject> query = ParseQuery.getQuery("code");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> provineList, ParseException e) {
            if (e == null) {
                for (ParseObject mProvine : provineList) {
                    Provine provine = new Provine();
                    provine.setPro((String) mProvine.get("provine"));
                    provine.setNumber((String) mProvine.get("code_number"));
                    provines.add(provine);
                    Log.d("All provine", provines.get(i).getPro()); (it's ok, no problem).
                    i++;
                }
            } else
                Log.d("Provines", "Error: " + e.getMessage());
            }
        }
    });
Log.d("All provine", provines.get(0).getPro()); (it's null ).

Upvotes: 2

Views: 496

Answers (1)

Amit Prajapati
Amit Prajapati

Reputation: 14315

You can declare a subclass which extends ParseObject.

Call ParseObject.registerSubclass(YourClass.class) in your Application constructor before calling Parse.initialize().

Follow this Subclasses https://www.parse.com/docs/android/guide#objects-subclassing-parseobject

      // Armor.java
    import com.parse.ParseObject;
    import com.parse.ParseClassName;

    @ParseClassName("Armor")
    public class Armor extends ParseObject {
    }

    // App.java
    import com.parse.Parse;
    import android.app.Application;

    public class App extends Application {
      @Override
      public void onCreate() {
        super.onCreate();

        ParseObject.registerSubclass(Armor.class);
        Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
      }
    }

On Query you get that data as ArrayList.

ParseQuery<Armor> query = ParseQuery.getQuery(Armor.class);
query.whereLessThanOrEqualTo("rupees", ParseUser.getCurrentUser().get("rupees"));
query.findInBackground(new FindCallback<Armor>() {
  @Override
  public void done(List<Armor> results, ParseException e) {

    // here you can use results same as object model.


  }
});

Upvotes: 1

Related Questions