0

我正在使用AsyncTask并异步获取 twitter 提要以显示在 listView 中。它工作正常然后为了刷新列表视图,我将 asyncTask 调用放入 aRunnableRun()以在特定间隔后加载它。提要在特定时间间隔后加载,但列表视图未更新。我notifyDataSetChanged()在 asyncTask 内部使用,但它不起作用。

这是我的尝试代码:

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();
        }
    }
}
4

3 回答 3

0

您是否尝试过移动这条线:

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

在 onCreate 方法中的 setContentView 之后?

于 2013-07-25T06:54:51.117 回答
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);
    }
}

这样你就可以避免使用AsyncTaskinside a TimerTask

于 2013-07-25T07:20:53.023 回答
0
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets);

取消注释 onPostExecute() 方法中的此代码。我想它可能会帮助你。谢谢...

于 2013-07-25T07:09:51.430 回答