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 Build a Self-Destructing Message Android App Self-Destruction Maintaining Scroll Position in a ListView

MessageAdapter error - cannot cast

Hi all,

I have just added the line of code that reloads the MessageAdapter if it does not already exist:

((MessageAdapter)getListView().getAdapter()).refill(mMessages);

I get an error in LogCat that says:

ClassCastException - android.widget.ArrayAdapter cannot be cast to MessageAdapter

This error is pointing at line 65 of the InboxFragment which is the line of code above.

Any thoughts?

Thanks,

Steve.

5 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Okay, the first thing I notice is that our import statements are different. You aren't importing MessageAdapter, but you are importing ArrayAdapter. Try this:

Delete:

import android.widget.ArrayAdapter;

And add:

import uk.co.bigdogconsultants.ribbit.adapters.MessageAdapter;

(or whatever your package name is)

Ah, and now I see that you are still using an ArrayAdapter instead of MessageAdapter. Change this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                getListView().getContext(), 
                                android.R.layout.simple_list_item_1,
                                usernames);
                        setListAdapter(adapter);

to this:

MessageAdapter adapter = new MessageAdapter(
                                getListView().getContext(), 
                                mMessages);
                        setListAdapter(adapter);
Tom Wierman
Tom Wierman
3,002 Points

This works in my project:

import android.support.v4.widget.SwipeRefreshLayout;

                } else {
                    //refill the adapter
                    ((MessageAdapter) getListView().getAdapter()).refill(mMessages);

That's the same as my code, so the error must lie elsewhere. It's strange as it compiles and build & even runs but fails during runtime with the above error.

Maybe I'll get lucky and Ben Jakuben will have a look!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

What class is your MessageAdapter extending?

HI Ben,

I've got:

public class MessageAdapter extends ArrayAdapter<ParseObject>{

Steve.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Matches mine! Can you paste in all your code from InboxFragment.java?

This is the code ...

package uk.co.bigdogconsultants.ribbit;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class InboxFragment extends ListFragment {

    protected List<ParseObject> mMessages;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_inbox, container, false);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setProgressBarIndeterminate(true);
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
        query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId());
        query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> messages, ParseException e) {
                getActivity().setProgressBarIndeterminateVisibility(false);

                if(e == null) {
                    // we found messages
                    mMessages = messages;
                    String[] usernames = new String[mMessages.size()];
                    int i = 0;
                    for(ParseObject message : mMessages) {
                        usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
                        i++;
                    }
                    if(getListView().getAdapter() == null) {
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                getListView().getContext(), 
                                android.R.layout.simple_list_item_1,
                                usernames);
                        setListAdapter(adapter);
                    }
                    else {
                        // refresh the adapter
                        ((MessageAdapter)getListView().getAdapter()).refill(mMessages);
                    }
                }

            }
        });
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);

        ParseObject message = mMessages.get(position);
        String messageType = message.getString(ParseConstants.KEY_FILE_TYPE);
        ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);
        Uri fileUri = Uri.parse(file.getUrl());

        if(messageType.equals(ParseConstants.TYPE_IMAGE)) {
            // view image
            Intent intent = new Intent(getActivity(), ViewImageActivity.class);
            intent.setData(fileUri);
            startActivity(intent);

        }   
        else {
            // view video
            Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
            intent.setDataAndType(fileUri, "video/*");
            startActivity(intent);
        }


    }
}

Thanks,

Steve.

That all works much better - I don't get an error now. Thanks, Ben :)

However, the Inbox items have no name next to them and the image disappears quickly. I may be confusing the iOS app & the Android one, but I didn't think we'd done the self-destruct yet!! (And my iOS app shows the recipient, not the sender!!)

Steve.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Doh! Take a shot at troubleshooting it and then post any follow-up questions in here. :)