0

I have a list which I am trying to broadcast with the use of intents. After following online tutorials, I was adviced to use Parcelable in order to send this data. However, I keep getting this error in logcat:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable

from this line of code bundle.putParcelable("data", (Parcelable)tweets); I do not know how to correct this.

Where i am building the intent

    protected void onHandleWork(@NonNull Intent intent) {
        Log.d(TAG, "onHandleWork: ");
        List<tweet> tweets = new ArrayList();

        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer("HyjgZgfiqSODTdICZUXIHI8HK", "TlynMItosq99QxnLMLGxA6FElD3TAKx9UmBxva5oExg9Gz1mzV");
        AccessToken accessToken = new AccessToken("2362719277-w5QlRNB2I7PXdMJuDXf5cc8FDT5H8X38ujxrtiT", "3v2Z2cqezaFrV6pFHu2yfPVFHZgMvLjMVKH4cUujI9kwI");
        twitter.setOAuthAccessToken(accessToken);
        Query query = new Query("Twitch");
        try {
            QueryResult result = twitter.search(query);
            for (Status status : result.getTweets()) {
                String createdat = status.getCreatedAt().toString();
                String text = status.getText();
                String retweets = String.valueOf(status.getRetweetCount());
                String favs = String.valueOf(status.getFavoriteCount());
                String uri = status.getUser().getProfileImageURL();
               tweet onetweet = new tweet(createdat,text,retweets,favs,uri);

              //  Log.d(TAG, status.getText());
                tweets.add(onetweet);
            }
            if (isStopped()) return;
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        sendToUI(tweets);

    }

    private void sendToUI(List tweets) {
        Intent intent = new Intent("tweet_result");
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", tweets);
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }

My tweet POJO

import android.os.Parcel;
import android.os.Parcelable;

public class tweet implements Parcelable {
    private String created_at;
    private String text;
    private String retweet_count;
    private String favorite_count;
    private String image_uri;

    public String getImage_uri() {
        return image_uri;
    }


    public String getCreated_at() {
        return created_at;
    }

    public String getText() {
        return text;
    }

    public String getRetweet_count() {
        return retweet_count;
    }

    public String getFavorite_count() {
        return favorite_count;
    }


    protected tweet(Parcel in) {
        created_at = in.readString();
        text = in.readString();
        retweet_count = in.readString();
        favorite_count = in.readString();
        image_uri = in.readString();
    }

    public static final Creator<tweet> CREATOR = new Creator<tweet>() {
        @Override
        public tweet createFromParcel(Parcel in) {
            return new tweet(in);
        }

        @Override
        public tweet[] newArray(int size) {
            return new tweet[size];
        }
    };


    @Override
    public int describeContents() {
        return 0;
    }

    public tweet(String created_at, String text, String retweet_count, String favorite_count, String image_uri) {
        this.created_at = created_at;
        this.text = text;
        this.retweet_count = retweet_count;
        this.favorite_count = favorite_count;
        this.image_uri = image_uri;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(created_at);
        dest.writeString(text);
        dest.writeString(retweet_count);
        dest.writeString(favorite_count);
        dest.writeString(image_uri);
    }
}

David Wasser
  • 93,459
  • 16
  • 209
  • 274

2 Answers2

0

You used wrong method, you should use intent.putParcelableArrayListExtra() but don't forget about that your array list must contains only parcelables items.

easy_breezy
  • 1,073
  • 7
  • 18
  • firstly, when i made the change, i am now getting this error. `putParcelableArrayListExtra cannot be applied to (android.os.bundle)` Secondly, how do i make sure my arraylist contains only parcelables as you say? –  Feb 25 '20 at 10:24
  • You get the error, because you put your list into a bundle, it's wrong if you are using putParcelableArrayListExtra method because the method waits an arrayList of parcelables, not a bundle. As for second question in your case everything is fine your arrayList contains the instances only of Tweet class that implements Parcelable. – easy_breezy Feb 25 '20 at 11:23
0

Changing my sendToUI() to this has worked:

    private void sendToUI(List tweets) {
        Intent intent = new Intent("tweet_result"); //tweet_result is a string to identify this intent
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) tweets);
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }