Android Parse - 指向其他表的指针表,想要检索其他表中的对象

时间:2017-01-06 06:02:23

标签: android list parse-platform

我觉得我非常接近这一点,只需要最后一点。

我有以下表格:

_User(标准解析表)

类别(对象ID,名称)

练习(对象ID,名称,描述,缩略图,图像等)

和UserFavourites,这是我存储用户首选练习的地方

(objectId,user-> users table,exercise-> exercise table,category-> category table)

我已经使用指针写Parse了:

               //create new parse object
                ParseObject favouriteExercise = new ParseObject("UserFavourites");
                //create pointers to the Exercise table and Category table
                ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
                ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
                //put those pointers into the Userfavourites table and save
                favouriteExercise.put("user",ParseUser.getCurrentUser());
                favouriteExercise.put("exercise",exercise);
                favouriteExercise.put("category",category);
                //save
                favouriteExercise.saveInBackground();

现在我正在尝试检索用户喜欢的所有练习,并通过在表格中搜索与用户指向用户表格的指针匹配的任何对象将它们放入列表视图中:

    ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
    final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
    query.whereEqualTo("user", user);
    //call to parse.com to start the query

    query.findInBackground(new FindCallback<Exercises>() {
        @Override
        public void done(List<Exercises> exercises, ParseException e) {
            if (exercises != null) {
                Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
                mAdapter.clear();
                //add all the exercises to the list
                mAdapter.addAll(exercises);
                //sort the list alphabetically
                mAdapter.sort(new Comparator<Exercises>() {
                    @Override
                    public int compare(Exercises exercises, Exercises t1) {
                        return exercises.getName().compareTo(t1.getName());
                    }
                });
            } else {
                mNoFavourites.setVisibility(View.VISIBLE);
            }

我遇到的问题是,当我运行此操作时,我可以看到我的查询正在运行 - &gt;我正在检索UserFavourites中我喜欢的8行表中的4行,所以它正确过滤,但我得到的对象并没有指向我想要的练习。它们只是空指针。

感谢。

enter image description here

2 个答案:

答案 0 :(得分:1)

是的,它只返回引用(指针)。如果您想要实际对象数据,请调用fetchInBackground

myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
 if (e == null) {
  // Success!
 } else {
  // Failure!
 }
}
});

答案 1 :(得分:0)

我根据kishore jethava给出的逻辑来判断它。

我查询了收藏夹表格,然后查询了我想要的结果(指向另一个表格)我遍历每个结果并得到它指向的对象并将其添加到我的ArrayList中。

public void getFavourites() {
    //set progress bar
    setProgressBarIndeterminateVisibility(true);
    ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");

    final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
    query.whereEqualTo("user", user);

    query.include("exercise");
    //call to parse.com to start the query
    query.findInBackground(new FindCallback<Exercises>() {
        @Override
        public void done(List<Exercises> objects, ParseException e) {

            if (objects.size() != 0) {
                for(ParseObject object : objects)
                {
                    //for each pointer found, retrieve the object it points to
                    obj = object.getParseObject("exercise");
                    mAdapter.add((Exercises) obj);
                    }
                    });
                }
            } else {
                mNoFavourites.setVisibility(View.VISIBLE);
            }
            //stop progress bar
            setProgressBarIndeterminateVisibility(false);
        }
    });
}