2013-07-25 46 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内。