显示查询结果Parse.com android

时间:2015-07-08 10:54:42

标签: android parse-platform

我有一个问题,这可能听起来像是一个愚蠢的问题,但我真的不知道如何并且需要一个答案。

在完成查询后的parse.com中,如何显示结果

例如这个

ParseQuery<ParseObject> gameQuery = ParseQuery.getQuery("Game");
gameQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());

我想在我的应用中将查询结果设置为某个文本视图: textview.setText(??);

提前谢谢你:)

修改

这是我使用

的代码
public class Adapter extends ParseQueryAdapter<ParseObject> {
//MediaPlayer mediaPlayer;

public Adapter(Context context) {

    super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Game");
            return query;
        }
    });
}

// Customize the layout by overriding getItemView
@Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {

    if (v == null) {
        v = View.inflate(getContext(), R.layout.list_item, null);
    }

    super.getItemView(object, v, parent);

    // I'm getting Images from Game class
    ParseImageView audioImage = (ParseImageView) v.findViewById(R.id.list_image);
    ParseFile imageFile = object.getParseFile("img");
    if (imageFile != null) {
        audioImage.setParseFile(imageFile);
        audioImage.loadInBackground();
    }//end if


    //retrieving Titles from Game Class
    TextView titleTextView = (TextView) v.findViewById(R.id.list_title);
    titleTextView.setText(object.getString("title"));


    //Now I want to retrieve name from User Class :(
    // the code is crashing my app
    //?????????????????????????????????????????????????

    ParseQuery<ParseObject> userQuery = ParseQuery.getQuery("Game");
    userQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());
    userQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, com.parse.ParseException e) {

            if (e == null) {
                ParseObject userName = list.get(0); // Gets first object

                // You can get fields using the get() method
                ParseUser user = (ParseUser) userName.get("createdBy");

                // There are data specific methods as well
                String firstName = user.getString("name");

                nameTextView.setText(firstName);
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }

    });





    return v;
}//end getItem View

}//end custom Adapator

1 个答案:

答案 0 :(得分:0)

ParseQuery<ParseObject> gameQuery = ParseQuery.getQuery("Game");
gameQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());
gameQuery.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> gameList, ParseException e) {
        if (e == null) {
            ParseObject game = gameList.get(0); // Gets first object

            // You can get fields using the get() method
            ParseUser user = game.get("createdBy");

            // There are data specific methods as well
            String firstName = user.getString("userFirstName");

            textview.setText(firstName);
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
});

以上只是一个例子。有关从对象获取数据的所有ParseObject方法,请参阅documentation get*()

一些常见的有:get()getString()getBoolean等。

<强>更新

首先,您必须告诉您的查询以获取指针数据(默认情况下,Parse不包括查询的指针数据)。这就是你的应用程序崩溃的原因。因此,请将您的查询更新为:

ParseQuery<ParseObject> userQuery = ParseQuery.getQuery("Game");
userQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());
userQuery.include("createdBy");

更新2:

public class Adapter extends ParseQueryAdapter<ParseObject> {
    //MediaPlayer mediaPlayer;

    public Adapter(Context context) {

        super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
            public ParseQuery create() {
                ParseQuery query = new ParseQuery("Game");
                userQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());
                query.include("createdBy");
                return query;
            }
        });
    }

    // Customize the layout by overriding getItemView
    @Override
    public View getItemView(final ParseObject object, View v, ViewGroup parent) {

        if (v == null) {
            v = View.inflate(getContext(), R.layout.list_item, null);
        }

        super.getItemView(object, v, parent);

        // I'm getting Images from Game class
        ParseImageView audioImage = (ParseImageView) v.findViewById(R.id.list_image);
        ParseFile imageFile = object.getParseFile("img");
        if (imageFile != null) {
            audioImage.setParseFile(imageFile);
            audioImage.loadInBackground();
        }//end if


        //retrieving Titles from Game Class
        TextView titleTextView = (TextView) v.findViewById(R.id.list_title);
        titleTextView.setText(object.getString("title"));


        //Now I want to retrieve name from User Class :(
        // the code is crashing my app
        //?????????????????????????????????????????????????

        // You can get fields using the get() method
        ParseUser user = (ParseUser) object.get("createdBy");

        // There are data specific methods as well
        String firstName = user.getString("name");

        nameTextView.setText(firstName);

        return v;
    }//end getItem View

}//end custom Adapator