简体   繁体   English

在解析中查询一对多关系

[英]Querying a one to many relationship in parse

Hello im trying to query a one to many relationship in parse. 您好我正在尝试查询解析中的一对多关系。 I have an Event table and invitation Table. 我有一个活动表和邀请表。 An event has many invitations and many invitations are created by an event. 一个事件有很多邀请,并且一个事件创建了许多邀请。 The Event table has a column called status . 事件表具有称为status的列。 Status has a value of 1 which means the owner has. 状态的值为1 ,表示所有者拥有。 In invitation table i have a column also called status and others users if going to an event will update the status to 1. I was successful in creating the table. 在邀请表中,我有一列也称为状态,如果参加某个活动,其他用户会将状态更新为1。我成功创建了表。 Now i am trying to attempt to query both Event and Invitation table where the status is 1 . 现在,我尝试尝试查询状态为1 Event表和Invitation表。 But i am having trouble accomplishing this task. 但是我在完成这项任务时遇到了麻烦。 Below is my code: 下面是我的代码:

 ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
        // Restrict to cases where the author is the current user.
        //pass in a ParseUser and not String of that user

        query.whereEqualTo("author", ParseUser.getCurrentUser());
        query.whereEqualTo("Status", "1");
        query.include("EventTitle");

        ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Invitation");
        parseQuery.whereMatchesQuery("EventId", query);


      //  query.orderByAscending("createAt");

        // Run the query
        parseQuery.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objectList, ParseException e) {
                if (e == null) {
                    // If there are results, update the list of event and notify the adapter
                    Log.d(TAG, "Im in background");
                  //  eventList.clear();
                    invitationList.clear();
                    for (ParseObject event : objectList) {
                        invitationList.add((Invitation)event);
                    }
                    Log.d(TAG, String.valueOf(invitationList.size()));

                    updateEventsList();

                } else {
                    Log.d(TAG, "Event retrieval error: " + e.getMessage());
                }
            }
        });
    }

And when i try to display in a ListView: 当我尝试在ListView中显示时:

TextView tvTitle = (TextView)convertView.findViewById(R.id.textView_eventTitle);

Error: i get a null pointer indicating a fetch Might be needed. 错误:我得到一个空指针,指示可能需要获取。 Please what i'm doing wrong 请我做错了

I believe you might have two options: 我相信您可能有两种选择:

 ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
 query.whereEqualTo("author", ParseUser.getCurrentUser());
 query.whereEqualTo("Status", "1");


ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Invitation");
parseQuery.whereEqualTo("EventId", id)

List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(query);
queries.add(parseQuery);

ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
  public void done(List<ParseObject> results, ParseException e) {
    // results has the list of of invitations/events.
  }
});

You can also try and save the eventId to the invitations table, that way you will not have to query two tables. 您也可以尝试将eventId保存到邀请表中,这样就不必查询两个表。 That will however, duplicate some data, which you may or may not be okay with. 但是,这将重复某些数据,您可能会或可能不会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM