2013-07-25 73 views
0

我正在使用AsyncTask並異步獲取twitter feed以顯示在listView中。它工作正常然後,刷新列表視圖我把asyncTask調用到RunnableRun()加載它在一個特定的時間間隔之後。 Feed在特定時間間隔後加載,但listview未更新。我在asyncTask中使用notifyDataSetChanged(),但它不起作用。如何在AsyncTask中刷新ListView?

這是我試圖代碼:

public class Twitter7FeedActivity extends Activity { 

    LazyAdapter adapter; 
    ListView list; 
    JSONObject jsonObj = null; 
    JSONArray jsonArray = null; 

    @SuppressLint("SimpleDateFormat") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_twitter7_feed); 
     callAsynchronousTask(); 
    } 

    public void callAsynchronousTask() { 
     final Handler handler = new Handler(); 
     Timer timer = new Timer(); 
     TimerTask doAsynchronousTask = new TimerTask() { 

      @Override 
      public void run() { 
       handler.post(new Runnable() { 
        public void run() { 
         try { 
          new GetFeedTask().execute(CommonUtils.BEARER_TOKEN, 
            CommonUtils.URL); 
         } catch (Exception e) { 
         } 
        } 
       }); 
      } 
     }; 

     timer.schedule(doAsynchronousTask, 0, 50000); 
    } 

    protected class GetFeedTask extends AsyncTask<String, Void, String> { 
     private ProgressDialog dialog = new ProgressDialog(
       Twitter7FeedActivity.this); 

     protected void onPreExecute() { 
      this.dialog.setMessage("Please wait"); 
      this.dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // related code 
     } 

     @Override 
     protected void onPostExecute(String jsonText) { 
      // related code 
      adapter = new LazyAdapter(Twitter7FeedActivity.this, 
      // tweets); 
      list = (ListView) findViewById(R.id.list); 
      list.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
} 
+0

使用通知適配器方法 – KOTIOS

+1

他已經在使用'adapter.notifyDataSetChanged();' –

+0

可能是你想試試這個解決方案嗎? http://stackoverflow.com/questions/15472539/refresh-spinner-view-after-execute-asynchronous-task-in-another-class – tsohtan

回答

0

您是否嘗試過移動這條線:

list = (ListView) findViewById(R.id.list); 

在onCreate方法您的setContentView後?

+0

是的,我已經嘗試過,但不工作:( –

+0

@Noor Azam,你的答案是不是一個答案,將這種類型的混淆/建議放在註釋部分。 –

0
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets); 

取消對onPostExecute()方法中的此代碼的註釋。我認爲這可能會幫助你。謝謝...

0

你可以只使用Handler做你提問中提到的工作。事情是這樣的:

public class Twitter7FeedActivity extends Activity { 
LazyAdapter adapter; 
ListView list; 
JSONObject jsonObj = null; 
JSONArray jsonArray = null; 

@SuppressLint("SimpleDateFormat") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_twitter7_feed); 
    callAsynchronousTask(); 
} 

public void callAsynchronousTask() { 
    final Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 

     ProgressDialog dialog; 

     public void run() { 
        try { 

         //Show your dialog here 
         runOnUiThread(new Runnable() { 
          public void run() { 
           runnable.this.dialog = new ProgressDialog(Twitter7FeedActivity.this); 
           runnable.this.dialog.setMessage("Please wait"); 
           runnable.this.dialog.show(); 
          } 

         }); 

         //Do your AsyncTask's doInBackground work here 

         //Update the UI with runOnUiThread or using the Ui threa handler 
         runOnUiThread(new Runnable() { 
          public void run() { 
           // related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets); 
           list = (ListView) findViewById(R.id.list); 
           list.setAdapter(adapter); 
           adapter.notifyDataSetChanged(); 
          } 

         }); 

         //Then post the thread to excecute after 50000 milliseconds. 
         handler.postDelayed(runnable, 50000); 
        } catch (Exception e) 
        { } 
       } 
    }; 
    handler.post(runnable); 
    } 
} 

這樣你可以避免使用AsyncTask一個TimerTask內。