How to embedd user timeline

android

#1

Hi, I’m new at Fabric. I have read documention but it didn’t make me clear.

I have integrated Fabric to my application and i can login successfully. After login i want to show user timeline on another activity.My main activity is for login.Here is code with layout xml.

MainActivity.xml

<com.twitter.sdk.android.core.identity.TwitterLoginButton
    android:id="@+id/twitter_login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new Twitter(authConfig));
    setContentView(R.layout.activity_main);

    loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
    loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            // Do something with result, which provides a TwitterSession for making API calls
            Toast.makeText(getApplicationContext(), "Login Succeed", Toast.LENGTH_LONG).show();

             Intent timeLine = new Intent(MainActivity.this, Timeline.class); 
             startActivity(timeLine);

        }

Timeline.xml

<TextView android:id="@id/android:empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal|center_vertical"
    android:text="No Tweets"/>

<ListView android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="#e1e8ed"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="false"/>

Timeline.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeline);

        final UserTimeline userTimeline = new UserTimeline.Builder()
                .screenName("fabric")
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
                .setTimeline(userTimeline)
                .build();
        setListAdapter(adapter);
    }

#2

your loginButton.setCallback method should like below code (That is, you should send username to Timeline.class via intent.putExtra and get username in Timeline.class via getIntent().getStringExtra)
MainActivity.java

loginButton.setCallback(new Callback<TwitterSession>() {

            @Override
            public void failure(TwitterException arg0) {
                Toast.makeText(MainActivity.this, "ERROR:"+arg0, Toast.LENGTH_LONG);
                
            }

            @Override
            public void success(Result<TwitterSession> arg0) {
                
                Intent intent = new Intent(MainActivity.this, Timeline.class);
                intent.putExtra("username", arg0.data.getUserName());
                startActivity(intent);
            }
        });

Timeline.java

final UserTimeline userTimeline = new UserTimeline.Builder().screenName(getIntent().getStringExtra("username")).build();
        
        adapter = new TweetAdapter(this, userTimeline);
        
        setListAdapter(adapter);

#3

Thanks for reply. But how to use TweetAdapter ? I wrote as you had written. But it gave me error. My time class extends from ListActivity. Do i need extend another class ? I wrote following code but it doesn’t work.

TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(this, userTimeline);


#4

Oh, sorry;
İ wrote my own adapter class which extended TweetTimelineListAdapter. But you can write following code for class Timeline extends ListActivity.

final UserTimeline userTimeline = new UserTimeline.Builder()
            .screenName(getIntent().getStringExtra("username"))
            .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
            .setTimeline(userTimeline)
            .build();
        setListAdapter(adapter);

#5