grantespo
grantespo

Reputation: 2269

ListView doesn't scroll to bottom after adding item

The Activity contains an edit text, submit button, and a list view that gets its data from a server. When I click, the edit Text, it scrolls to the bottom as it is supposed to:

  editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

            getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            scrollMyListViewToBottom();
            Log.i("yoyo","ListSize Before: " + size);
    }
});

However, when I click on the submit Button, and after the list view updates when notifydatasetchanged(); is called, It does not scroll to the bottom. If you are wondering what happens in CommentQuery();, I can provide more code.

    submitComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            commentItem = new ParseObject("CommentItem");
            commentItem.put("parentUser", feedUserName);
            commentItem.put("parentFeed", feedItem);
            // commentItem.put("parentObjectId", objectId);
            commentItem.put("commentText", String.valueOf(commentText.getText()));
            commentItem.put("username", ParseUser.getCurrentUser().getUsername());
            commentItem.put("country", ParseUser.getCurrentUser().getInt("country"));
            commentItem.put((ParseUser.getCurrentUser().getUsername() + "globalPoints"), 0);
            commentItem.put("parentObjectId", objectId);
            replies +=1;


            commentItem.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        arrayCommentList.clear();
                        comments.clearCachedResult();
                        CommentQuery();
                    customCommentListViewAdapter.notifyDataSetChanged();

                        Log.i("yoyo","ListSize After: " + size);   //size changes like it is supposed to

                        scrollMyListViewToBottom(); //doesn't do anything
                    }
                }
            });

        }
    });

scrollMyListViewToBottom() Function:

   public void scrollMyListViewToBottom() {
    commentList.post(new Runnable() {
        @Override
        public void run() {
            // Select the last row so it will scroll into view...

            commentList.setSelection(size);
        }
    });

}

CommentQuery Function:

public void CommentQuery(){

    comments = new ParseQuery<>("CommentItem");
    comments.setLimit(99);
    comments.whereEqualTo("parentObjectId", objectId);
    comments.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> mobjects, ParseException e) {

            if(e == null){

                for(ParseObject object : mobjects){

                    parseObs = mobjects;

                     size = parseObs.getsize();

                    commentData = new HashMap<>();
                    commentData.put("username", object.getString("username"));
                    commentData.put("feed", object.getString("commentText"));
                    commentData.put("likes", String.valueOf(object.getInt("likes")));
                    commentData.put("country", String.valueOf(object.getInt("country")));
                    commentData.put("replies", String.valueOf(0));
                    commentData.put("global", String.valueOf(object.getInt(usernameText+"globalPoints")));

                    arrayCommentList.add(commentData);

                }
                customCommentListViewAdapter = new CustomCommentListViewAdapter(getApplicationContext(), arrayCommentList);
                commentList.setAdapter(customCommentListViewAdapter);
            }

        }
    });

}

Upvotes: 1

Views: 146

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191844

First - Don't do this. parseObs = mobjects; whatever adapter you just set using parseObs just lost the reference to that list.

You instead need to do this.

parseObs.clear();
parseObs.addAll(mobjects);

Next, size = parseObs.getsize(); definitely does not need to be within the loop. The list shouldn't change in size while you are iterating over it.


Finally, this already does what you want inside of the Parse Callback

customCommentListViewAdapter = new CustomCommentListViewAdapter(getApplicationContext(), arrayCommentList);
commentList.setAdapter(customCommentListViewAdapter);

You do not need to notify the adapter again.

CommentQuery();
// customCommentListViewAdapter.notifyDataSetChanged(); // Not necessary

Then, CommentQuery is asynchronous.

arrayCommentList.clear(); // List is now empty
... 
CommentQuery(); // doing stuff ... in the background
..
scrollMyListViewToBottom(); // there isn't anything to scroll to yet!

Basically, solution is to scrollMyListViewToBottom() within CommentQuery()'s done { } method block, which would be after the ListView contains the data.

And, as I said in the comments, size is not necessary as a variable. Just use customCommentListViewAdap‌​ter.getCount()

Upvotes: 1

Related Questions