如何将ParseObject强制转换为ParseUser

时间:2013-05-31 11:00:21

标签: android parse-platform

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

        // 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"));

2 个答案:

答案 0 :(得分:3)

使用现有的&#34;用户&#34; Parse中的类,但使用

对其进行子类化
@ParseClassName("_User")
public class PUser extends ParseUser {

按照article。你实际上特别需要参考 _User &#34; ParseClassName&#34;当子类化这个对象时。它超级毛茸茸,我被困在这里多年了,因为对于其他课程你只需要注册&#34;根据Parse数据浏览器使用Parse使用它的文字名称的类,在这种情况下是&#34; User&#34;,&#34; Info&#34;,&#34; Post&#34;等,但用户类 特别需要下划线

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

我知道这是一个古老的问题,但这对我来说并不清楚,并且在我所遇到的问题上没有一个完整的主题。希望这有帮助。

答案 1 :(得分:0)

我认为你不需要&#34;演员&#34;对于ParseUser来说,parseuser也是一个parseobject,除了附加了一些基本的预定义属性,你可以像查询任何其他parseobject一样查询