androidparse-platformandroid-parsequeryadapter

Android ParseQueryAdapter always empty


I am trying to get data from Parse.com, and followed instrucions and examples on their site, but i run into problem i can't seem to fix. Requested query is always empty. I use following code

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);
}

Here adapterEmpty always returns "true", and listView isn't updated.

CommentPost class:

@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);
}
}

When i do this code (from tutorial on Parse website):

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

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

it's working fine, I am getting all saved comments for class "data", but i need customised query. Any help would be greatly appreciated:)


Solution

  • Got it to work. Here's the code:

    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;
            }
        };