Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

variable inside refil() method

I just finished Messaging app project in Android app track, but there are some questions and this is one of them.

Inside the fragment class for Inbox( in the video, InboxFragment), I'm supposed to refil the adapter that is for the list of messages. To achieve this, I call refil() method from the custom adapter class that is for setting custom adapter for the list instead of one of android.R.layout.* lists. I call this method with mMessages which is already loaded with the list of messages returned by done() method? as the callback of findInBackground of ParseQuery like this:

((InboxCustomLayoutAdapter)getListView().getAdapter()).refil(mMessages);

So I assume that the following code's mMessages variable, which is also loaded with the same list as I mentioned above, could be replaced with "messages", because inside the refil(List<ParseObject> messages), in this case, method, every List of ParseObject could be or should be replaced as "messages".

public void refil(List<ParseObject> messages) {
        mMessages.clear();
        mMessages.addAll(messages);
        notifyDataSetChanged();
    }

But it does not work that way. When I change mMessages to messages, the list does not get refilled and to end up with leacing a bunch of index error items: "there is no such thing" items on display.

If anyone has an idea why this is a bad idea.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Okay, that's what I thought. The messages variable here is being passed in as the new list of messages. The member variable mMessages, is what is used by the adapter to display in the ListView. So the purpose of this refill() method is to update the mMessages member variable with the new list of messages and then force an update in the display. If you clear the messages variable in the first line, then you're clearing the new list that you just passed in. Then you won't have anything to add or display.

i think I got it. So we're trying to replace mMessages, that is the data now displayed on the screen, with messages, =mMessages new version that is the data updated in onResume, correct?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Yep! mMessages will always be what is displayed by the adapter, so we are updating that with the new messages.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you paste in the code you are trying that isn't working? I have a hunch but don't know if I'm understanding correctly what you are trying.

public void refil(List<ParseObject> messages) {
        messages.clear();
        messages.addAll(messages);
        notifyDataSetChanged();
    }

What I'm trying to do is write messages instead of mMessages, because I think messages in refil() method is working as any of List<ParseObject>sent as parameter. So I wonder why .refil(mMessages) does not mean messages is same as mMessages.