2016-06-22 73 views
0

我想要獲取推文列表,但結果tweets列表必須是最終的,這是一個問題,因爲我想返回list.World的結果是可能的返回結果一旦填滿?推文列表si allways null

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) { 

    } 
} 

這是CLAS whre我得列表

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

方法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) { 

     } 
    }); 
} 

然後,你可以顯示Twitter或做你想做的事,這個方法:

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

我已經編輯第一psost與我也做了改變 – AFS

+0

你做了和你一樣的邏輯,但以其他方式。嘗試做我發佈的內容 –