1

我正在尝试从 Parse.com 获取数据,并按照他们网站上的说明和示例进行操作,但遇到了我似乎无法解决的问题。请求的查询始终为空。我使用以下代码

public void createParseQuery(final int markerId){
ParseQueryAdapter.QueryFactory<CommentPost> queryAdapterFactory =
    new ParseQueryAdapter.QueryFactory<CommentPost>() {
        public ParseQuery<CommentPost> create() {
        ParseQuery<CommentPost> parseQuery = CommentPost.getQuery();
        parseQuery.whereEqualTo("markerId", markerId);
        parseQuery.include("comment"); //comment define
        parseQuery.include("rating");
            return parseQuery;
        }
    };
    queryAdapter = new ParseQueryAdapter<CommentPost>(this, queryAdapterFactory) { 
        @Override
        public View getItemView(CommentPost post, View view, ViewGroup viewGroup){
            if (view == null) {
                view = View.inflate(getContext(), R.layout.item_comment_post, null);
            }
            TextView textViewPostUsername = (TextView) view.findViewById(R.id.textViewPostUsername);
            TextView textViewPostComment = (TextView) view.findViewById(R.id.textViewPostComment);
            RatingBar ratingBarPostRating = (RatingBar) view.findViewById(R.id.ratingBarPostRating);
            textViewPostUsername.setText(post.getUser().getUsername());
            textViewPostComment.setText(post.getComment());
            ratingBarPostRating.setProgress(post.getRating());
            return view;
        }
    };
    Boolean adapterEmpty= queryAdapter.isEmpty();
    Log.i("DEBUG","Adapter is empty "+adapterEmpty);
    ListView lv = (ListView)findViewById(R.id.listView111);
    lv.setAdapter(queryAdapter);
}

这里adapterEmpty 总是返回“true”,并且listView 没有更新。

评论帖子类:

@ParseClassName("data")
public class CommentPost extends ParseObject {
ParseUser user;
int markerId;
String comment;
int rating;
public CommentPost() {
    super();
}
public String getComment() {
    return getString("comment");
}
public ParseUser getUser() {
    return getParseUser("user");
}
public int getRating() {
    return getInt("rating");
}
public void setComment(ParseUser user, int markerId, String comment,
        int rating) {
    put("user", user);
    put("markerId", markerId);
    put("comment", comment);
    put("rating", rating);
}
public static ParseQuery<CommentPost> getQuery() {
    return ParseQuery.getQuery(CommentPost.class);
}
}

当我执行此代码时(来自 Parse 网站上的教程):

mainAdapter = new ParseQueryAdapter<ParseObject>(this, "data");
mainAdapter.setTextKey("comment");

ListView lv = (ListView)findViewById(R.id.listView111);
lv.setAdapter(mainAdapter);

它工作正常,我正在为类“数据”获取所有保存的评论,但我需要自定义查询。任何帮助将不胜感激:)

4

1 回答 1

1

让它工作。这是代码:

public void createParseQuery(final int markerId) {

    ParseQueryAdapter.QueryFactory<CommentPost> queryAdapterFactory = 
            new ParseQueryAdapter.QueryFactory<CommentPost>() {
        public ParseQuery<CommentPost> create() {
            ParseQuery<CommentPost> parseQuery = CommentPost.getQuery();
            parseQuery.whereEqualTo("markerId", markerId);
            parseQuery.include("user");
            parseQuery.orderByDescending("createdAt");

            return parseQuery;
        }
    };

    queryAdapter = new ParseQueryAdapter<CommentPost>(this, queryAdapterFactory) {
        @Override
        public View getItemView(CommentPost post, View view, ViewGroup parent) {
            if (view == null) {
                view = View.inflate(getContext(),
                        R.layout.item_comment_post, null);
            }
            TextView textViewPostUsername = (TextView) view
                    .findViewById(R.id.textViewPostUsername);
            TextView textViewPostComment = (TextView) view
                    .findViewById(R.id.textViewPostComment);
            RatingBar ratingBarPostRating = (RatingBar) view
                    .findViewById(R.id.ratingBarPostRating);

            textViewPostUsername.setText(post.getUser().getUsername());
            textViewPostComment.setText(post.getComment());
            ratingBarPostRating.setProgress(post.getRating());
            return view;
        }
    };
于 2014-10-21T18:17:01.903 回答