0

I am trying to make an app with a twitter timeline an android studio. To do this I am using the twitter fabric API. The documentation for that I am following is found at https://docs.fabric.io/android/twitter/show-timelines.html#timeline-builders. The problem I am having is that the setListAdapter in setListAdapter(adapter); is coming up with an error saying "cannot resolve method 'setListAdapter(com.twitter.sdk.tweetui.TweetTimelineListAdapter)'". I really have no experience with android studio, so I've copied and pasted most of this code. I've looked up how adapters work in android studio, and I get what they do, but I'm not sure how they're set up. Thanks in advance for the help!

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.SwipeRefreshLayout;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetui.SearchTimeline;
import com.twitter.sdk.android.tweetui.TimelineResult;
import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;
import com.twitter.sdk.android.tweetui.UserTimeline;
import com.twitter.sdk.android.tweetui.CollectionTimeline;


public class FeedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);

        final SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        final SearchTimeline timeline = new SearchTimeline.Builder()
                .query("#fblaoutfit")
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
                .setTimeline(timeline)
                .build();
        setListAdapter(adapter);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeLayout.setRefreshing(true);
                adapter.refresh(new Callback<TimelineResult<Tweet>>() {
                    @Override
                    public void success(Result<TimelineResult<Tweet>> result) {
                        swipeLayout.setRefreshing(false);
                    }

                    @Override
                    public void failure(TwitterException exception) {
                        // Toast or some other action
                    }
                });
            }
        });
    }

Here's the XML if it will help you at all:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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"/>

</android.support.v4.widget.SwipeRefreshLayout>

1 Answers1

1

You need to study more on using ListViews and ListActivities in android.

Cause of Error

Your troubling method setListAdapter(adapter) is available in a ListActivity but your activity class is AppCompatActivity. There is no setListAdapter(adapter) method defined in AppCompatActivity. That is why you get the above error.

Solution

By considering rest of your implementation, it is fine that you use AppCompactActivity. But then you can't use the setListAdapter(adapter) method. Instead you need to get a reference to your ListView in your layout and set the adapter for the ListView as in below code snippet.

public class FeedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);

        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
            .setTimeline(timeline)
            .build();

        //obtain reference to the list view
        ListView listView = (ListView) findViewById(R.id.listView);

        //set the adapter to list view
        listView.setAdapter(adapter);

        //your rest of the code
    }
}

Also change the id of your ListView in layout file as follows.

<ListView android:id="@+id/listView"...
dishan
  • 1,346
  • 12
  • 21
  • Thanks! While I couldn't get your solution to work, changing the activity class to ListActivity did. I actually need to study more android, I've really only been using android studio for a week, it's nothing like unity. :) – Gtwogtwo Gtwogtwo Mar 16 '16 at 12:40