justinC19 February 2016

Grab Tweet string value using Fabric for android

I am working on an android app and I am new to the Fabric API.

I am trying to grab the string values of the tweets. I can populate a ListActivity with all the tweets(code taken from the documentation examples), but cannot figure out how to access that list to get the string values or even the tweet ids.

When i do this:

Log.d("", adapter.getItem(1).toString());

or

Log.d("", adapter.getItem(1));

it tells me "Invalid Index 1, size is 0"

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;
import com.twitter.sdk.android.tweetui.UserTimeline;

public class TwitterFeed extends ListActivity {
    Intent intent = new Intent();
    TweetTimelineListAdapter test;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        String username = pref.getString("username", "NULL");
        super.onCreate(savedInstanceState);

        final UserTimeline userTimeline = new UserTimeline.Builder()
        .screenName(username).includeReplies(true).includeRetweets(true)
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
                .setTimeline(userTimeline)
                .build();
        setListAdapter(adapter);
        test = adapter;
    }
    @Override
    protected void onResume(){
        super.onResume();
        Log.d("ListItem", test.getItem(1).toString());
    }
}

Any help would be very much appreciated. Thanks ahead of time!

Answers


justinC19 February 2016

so after a bunch of research, i was able to grab the required information. You need to call this from another class. The reason it was returning 0 was because the list hadn't finished loading. I have this block in an onClick listener in a custom adapter.

    TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
// Can also use Twitter directly: Twitter.getApiClient()
                StatusesService statusesService = twitterApiClient.getStatusesService();
                statusesService.show(tweetID, null, null, null, new Callback<com.twitter.sdk.android.core.models.Tweet>() {
                    @Override
                    public void success(Result<Tweet> result) {
                        //Do something with result, which provides a Tweet inside of result.data
                        Log.d("Tweet Data", result.data.text);
                    }
                    public void failure(TwitterException exception) {
                      //Do something on failure
                    }
                });

Hope somebody gets use out of this.

Post Status

Asked in February 2016
Viewed 2,777 times
Voted 11
Answered 1 times

Search




Leave an answer