2012-08-06 34 views
0

我有一個從Twitter加載推文的AsyncTask。 我也有一個PullToRefresh ListView ...每當我拉它來刷新它,列表視圖立即清除,並且一旦數據已被加載,它將被填充到列表視圖中。在AsyncTask完成之前獲得清單

我在我的應用程序中的其他ListView都具有相同的東西(PullToRefresh和異步數據加載...)。在其他ListViews這不會發生。只在Twitter ListView上。我究竟做錯了什麼?

這裏是我的代碼:

public class TwitterDownloader extends AsyncTask<Void, Void, String> { 

    final Handler mHandler = new Handler(); 

    public TwitterDownloader() { 
    } 

    @Override 
    public void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     twitter4j.Twitter twitter = new TwitterFactory().getInstance(); 

     listTweets.clear(); 

      List<twitter4j.Status> statuses = null; 

      try { 
       statuses = twitter.getUserTimeline(
         MainActivity.TWITTER_USERNAME, new Paging(1, 50)); 
      } catch (TwitterException e) { 
       e.printStackTrace(); 
       Log.e(MainActivity.LOG_TAG, "TwitterException"); 
      } 

      try { 
       for (twitter4j.Status status : statuses) { 
        listTweets.add(status.getText()); 
       } 
      } catch (NullPointerException npe) { 
      } 
     return null; 
    } 

    @Override 
    public void onPostExecute(String unused) { 
     MyCustomAdapter myAdapter = new MyCustomAdapter(myContext, 
       R.layout.row_twitter, listTweets); 
     setListAdapter(myAdapter); 
     getListView().setTextFilterEnabled(true); 

     String lastUpdate = (new SimpleDateFormat(
       "HH:mm")).format(new Date()); 

     pullToRefreshView.onRefreshComplete(); 
     pullToRefreshView.setLastUpdatedLabel(getString(R.string.last_updated) + ": " 
       + lastUpdate); 
    } 
+0

嘗試通知適配器改變。 – 2012-08-06 10:36:07

+0

我到底該怎麼做?我需要在哪裏添加它?究竟如何? – user754730 2012-08-06 10:43:16

+0

將myAdapter.notifyDataSetChanged()放入onPostExcute();如果它不起作用,請顯示您的logcat(應該打印NullPointerException的堆棧跟蹤) – R4j 2012-08-06 10:55:09

回答

0

我終於在我再次填充我的列表之前添加所有我的clear()語句來修復它(這是在try catch中)。

所以我doInBackground方法中的新代碼:

try { 
      listTweets.clear(); 
      listUsernames.clear(); 
      listDates.clear(); 
      listImageURLs.clear(); 
      listURLsOfTweets.clear(); 
      for (twitter4j.Status status : statuses) { 
       listTweets.add(status.getText()); 
       listUsernames.add(status.getUser().getName()); 
       listDates.add(android.text.format.DateFormat 
         .getDateFormat(getApplicationContext()).format(
           status.getCreatedAt()) 
         + " " 
         + android.text.format.DateFormat.getTimeFormat(
           getApplicationContext()).format(
           status.getCreatedAt())); 
       listImageURLs.add(status.getUser().getProfileImageURL() 
         .toString()); 

       StringBuffer address = new StringBuffer(); 
       address.append("http://twitter.com/#!/"); 
       address.append(status.getUser().getScreenName()); 
       address.append("/status/"); 
       address.append(status.getId()); 

       listURLsOfTweets.add(address.toString()); 
} 
1

我不知道這不過的AsyncTask的doInBackground方法,你在做listTweets.clear();。獲得結果後,您正在向其添加數據。可能是這是造成問題。

+0

那麼這應該不壞,因爲它是doInBackground中的第一個調用否? – user754730 2012-08-06 10:48:02

+0

我想知道一點,因爲通常不允許修改doInBackground()方法中活動的UI,因爲它在單獨的線程中運行。如果listTwitts.clear()不引發異常,請檢查LogCat。 – 2012-08-06 10:53:34

+0

哇,看起來像刪除它做了工作!非常感謝! – user754730 2012-08-06 11:12:16

相關問題