Android-解析查询以获取所有帖子,并让用户随该帖子一起去 [英] Android - Parse query to get all posts and the users to go with the posts

查看:87
本文介绍了Android-解析查询以获取所有帖子,并让用户随该帖子一起去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个应用程序,用户可以在其中登录并将文本发布到我的Parse数据库中.所有帖子都保存在Posts类内的文本列中,该类中的另一列是user,它是指向我的User类的指针,该类有一个名为username的列,这是我要检索的用户名字符串.我已经设法查询数据库并检索text列中的所有数据,将它们全部放入一个字符串数组中,然后放入我应用程序中的listview中.

接下来要做的是通过查看帖子中的相关用户名列,检索文本列中的所有帖子以及发布每个帖子的用户的名称.
我可以在一个查询中完成所有操作,还是应该创建另一个查询来执行此操作?

I have made an application where users can log in and post text to my Parse database. All posts are held in a text column inside a Posts class, another column in this class is user which is a pointer to my User class which has a column called username, it is the username string that I would like to retrieve.
I've managed to query the database and retrieve all of the data within the text column, put it all into a string array and then into a listview in my app.

what I would like to achieve next is to retrieve all of the posts in the text column and the name of the user that posted each one by looking at the related username column within Posts.
Can I do this all in one query or should I create another query to do this?

解析类:
用户
| objectID(字符串)|用户名(字符串)|密码(字符串)|

帖子
| objectID(字符串)|文字(字串)|用户(Pointer< _User>)|

Parse classes:
User
| objectID (String) | username (String) | password (String) |

Posts
| objectID (String) | text (String) | user (Pointer<_User>) |

用户将启动应用程序,注册一个用户名和密码,这会将一行包含其用户名和密码的行添加到User类.然后,用户将创建一个帖子,该帖子将在Posts类中添加一行,其中包含文本和指向在用户列内发布该帖子的用户的指针.

the user would start app, register a username and password which adds a row to the User class containing their username and password. The user would then make a post which would add a row to the Posts class containing text and a pointer to the user that made the post within the user column.

查询和ListView代码:

query and ListView code:

    //create new ArrayList and ArrayAdapter
    final ArrayList<String> postList = new ArrayList<String>();
    final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row, postList);


    //create new ParseQuery selecting all data within 'text' in descending order
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
    query.selectKeys(Arrays.asList("text"));
    query.addDescendingOrder("text");

    //query
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> text, ParseException e) {

            if (e == null) {
                //query successful

                int listSize = text.size();

                //loop through each post in 'text'
                for(int i = 0; i < listSize; i++){

                    //add each post to textArray
                    textArray[i] = text.get(i).getString("text");
                }

                //add all data in textArray to ListView
                postList.addAll(Arrays.asList(textArray));
                lvText.setAdapter(listAdapter);
            }

            else {
                //query error

                Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
            }

        }
    });

推荐答案

这很简单,您可以使用

It's rather simple, you can use a .include on an existing query to "Include the [class you want to include] data with each [Class you're querying]" and you can call the queried object I believe as follow:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
query.include([field containing user reference]);

ParseObject parseUser = text.get(i).getParseObject(([field containing user reference]);

我希望这会有所帮助!

更新

我忘了提到我在Posts类中的'用户'列是指向我的User类中用户的objectID的指针.之内是用户名列,这是我想反馈给我的应用程序的信息.

"I forgot to mention that my 'user' column within the Posts class is a pointer to the objectID of the user within my User class. Within the User class is a username column, this is the information that I would like to feed back to my application.

ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
query.include("user");

String parseUserName = text.get(i).getParseObject(("user").getString("userName");

这可能不起作用,并出现一条错误消息,提示您必须刷新对象才能获取数据.但是首先尝试一下,看看它是否能神奇地工作,我知道一些其他技巧:

It might be that this does not work, with an error saying you have to refresh the object to get the data. But first try this and see if it works it magic, I know some other tricks which are:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
query.include("user");

query.findInBackground(new FindCallback<ParseObject>() {

    @Override
    public void done(List<ParseObject> text, ParseException e) {

        if (e == null) {
            ParseQuery<ParseUser> query = ParseQuery.getQuery("_User");
            query.whereEqualTo("objectId", text.get(0).getParseObject(""));
            query.findInBackground(new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> users, ParseException e) {

                if (e == null) {
                     String parseUserName = users.get(0).getString("userName");

                }
            });
            int listSize = text.size();

            //loop through each post in 'text'
            for(int i = 0; i < listSize; i++){

这篇关于Android-解析查询以获取所有帖子,并让用户随该帖子一起去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆