推文列表si allways null

时间:2016-06-22 15:48:39

标签: android twitter twitter-fabric

我试图获取推文列表,但结果推文列表必须是最终的,这是一个问题,因为我想用list.World返回结果一旦填充就返回结果??

public class FollowingActivity extends AppCompatActivity implements TweetFragment.OnFragmentInteractionListener{

    //ListView homeList;
    //SwipeRefreshLayout mySwipeRefreshLayout;
    FloatingActionButton fab;

    MyPagerAdapter mPagerAdapter;
    ViewPager mviewPager;
    private List<Fragment> listaFragments;
    TwitterConnect twc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_following);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        twc = new TwitterConnect();
        Result<List<Tweet>> result = twc.getHomeTimeLine();//result is always null


        listaFragments = new ArrayList<Fragment>();
        listaFragments.add(TweetFragment.newInstance(1));
        listaFragments.add(TweetFragment.newInstance(2));
        listaFragments.add(TweetFragment.newInstance(3));


        // Creamos nuestro Adapter
        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
                listaFragments);

        // Instanciamos nuestro ViewPager
        mviewPager = (ViewPager) findViewById(R.id.viewPager);

        // Establecemos el Adapter
        mviewPager.setAdapter(mPagerAdapter);

        fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(FollowingActivity.this, ComposeTweet.class);
                startActivity(i);
            }
        });

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

这就是我必须得到名单的分数

public class TwitterConnect {
    TwitterApiClient twitterApiClient;
    StatusesService statusesService;
    Result<List<Tweet>> tweets;

    public TwitterConnect(){
        twitterApiClient = TwitterCore.getInstance().getApiClient();
        statusesService = twitterApiClient.getStatusesService();
    }

    private void callHomeTimeLine(){

        statusesService.homeTimeline(200, null, null, null, null, null, null, new Callback<List<Tweet>>() {
            @Override
            public void success(Result<List<Tweet>> result) {
                setHomeTimeLine(result);
            }

            @Override
            public void failure(TwitterException exception) {
                Log.e("FAILURE", "Fallo en callHomeTimeLine");
            }
        });

    }

    private void setHomeTimeLine( Result<List<Tweet>> result){
        this.tweets = result;
    }

    private Result<List<Tweet>> getTweets() {
        return this.tweets;
    }

    public Result<List<Tweet>> getHomeTimeLine(){
        callHomeTimeLine();

        return getTweets();
    }
}

1 个答案:

答案 0 :(得分:1)

方法success()是异步的,所以你必须等到它被调用才能返回推文列表。

你可以这样做:

public void getHomeTimeLine(){
    statusesService.homeTimeline(200, null, null, null, null, null, null, new Callback<List<Tweet>>() {
        @Override
        public void success(Result<List<Tweet>> result) {
            displayTweets(result);
        }

        @Override
        public void failure(TwitterException exception) {

        }
    });
}

然后你可以在这个方法中显示推文或做你想做的事情:

private void displayTweets(Result<List<Tweet>> tweets) {
    // Your logic
}