在AsyncTask完成执行后,ExpandableListView未更新

时间:2011-05-24 19:45:46

标签: android android-asynctask expandablelistview

我有一个ExpandableListView作为活动的一部分创建:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.news);

    // create the exandable list view widget
    newsListAdapter = new NewsListAdapter(this.getApplicationContext());    

    newsListView = (ExpandableListView) findViewById(R.id.news_list_view);

    newsListView.setAdapter(newsListAdapter);

    final Button homeButton = (Button) findViewById(R.id.home_btn);

    // go back to the home page
    homeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {                
            startActivity(new Intent(view.getContext(), HomeActivity.class));
        }
    });   

    newsListView.setOnGroupClickListener(new OnGroupClickListener() {
        public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long arg3) {
            return false;
        }
    });

    newsListView.setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View view, 
                                    int groupPosition, int childPosition, long id) {
            return false;
        }
    });

    showLoadingProgressDialog();

    downloadRssFeedTask = new DownloadRssFeedTask();

    downloadRssFeedTask.execute();
}  

从代码中可以看出,它会在AsyncTask中从RSS提要中拉出来。

AsyncTask结束,取消进度对话框,并告知活动任务已完成:

   private void responseReceived(Channel newsResponse) {
    if (newsResponse != null)           
        newsListAdapter.buildView(newsResponse);
    }
    else {
    Toast toast = Toast.makeText(this.getApplicationContext(), 
                           "There is no news available at this time.", 
                           Toast.LENGTH_LONG);

        toast.show();
    }
}

但是,在加载数据后,ExpandableListView不会重绘/刷新,它是一个空白屏幕。你如何让ExpandableListView用RSS数据刷新自己?我尝试了invalidaterefreshDrawableState,但没有任何快乐。

思想?

2 个答案:

答案 0 :(得分:3)

我想您在下载RSS Feed后忘记在适配器上调用notifyDataSetChanged()方法(可能您应该在onPostExecute()中使用AsyncTask方法调用它。)

答案 1 :(得分:1)

这应该有用。

newsListAdapter.notifyDataSetChanged()