2

嗨,我想知道如何从 ParseObject 获取 ParseUser 对象。我需要这样做,因为 ParseQuery 返回一个列表。这是我的代码,感谢您的帮助!

        // get the currentUser
        currentUser = ParseUser.getCurrentUser();
        List<ParseObject> friendsList = currentUser.getList("friendsList");

        // search for the person the user wants to add as a friend
        List<ParseObject> userResults = new ArrayList<ParseObject>();
        ParseQuery otherUserQuery = ParseUser.getQuery();
        otherUserQuery.whereEqualTo("username", "jyo2");
        try {
            userResults = otherUserQuery.find();
        } catch (ParseException e1) {
            // fail
            e1.printStackTrace();
        }

        // get the friend from the query if there was one and add it to the
        // currentUser friends list
        if (userResults.size() != 0) {
            ParseObject currentObject = userResults.get(0);
            friendsList.add(currentObject);
            currentUser.put("friendsList", friendsList);
        }
        // save the update
        try {   
            currentUser.save();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // try to view the friends
        List<ParseObject> currentFL = currentUser.getList("friendsList");
        ParseObject currentPU = currentFL.get(0);
        System.out.println("THIS IS SIZE" + currentFL.size());
        System.out.println(currentPU.get("name"));
4

2 回答 2

3

在 Parse 中使用现有的“用户”类,但使用它进行子类化

@ParseClassName("_User")
public class PUser extends ParseUser {

根据这篇文章。在子类化此对象时,您实际上特别需要引用_User "ParseClassName"。这太麻烦了,我被困了很久,因为对于其他类,您只需要使用 Parse 数据浏览器中的字面名称“注册”该类,在这种情况下是“用户”,“信息”, “发布”等,但用户类特别需要下划线

编辑对不起,我应该提到,然后PUser当您从查询中获取返回的对象时,您只需转换子类。您可能还需要将查询更改为用户查询而不是对象查询。

我知道这是一个老问题,但这对我来说并不清楚,关于我遇到的问题也没有完整的线索,关于 SO。希望这有帮助。

于 2015-06-28T14:19:42.163 回答
0

我认为您不需要“强制转换”为 ParseUser,parseuser 也是一个 parseobject,除了附加一些基本的预定义属性之外,您可以像查询任何其他 parseobject 一样查询这些属性

于 2014-01-21T08:14:32.223 回答